Apply Container/Presenter pattern
If you have used React, you could be fimilar with Container/Presenter pattern.The presenter component is responsive for rendering view and notifying event handlers to handle events.
The container component handles events, interacts with external resources, eg, in a React application, the container is the glue codes with Redux.
In this post, I just try to split component into small components by different responsibilities.
Go back to post detail component as an example.
The post details view includes three sections:
- Post details
- Comment list of this post
- A Comment form used for adding new comment
- PostDetailCard
- PostCommentList
- PostCommentForm
<post-detail-card post="$ctrl.post"></post-detail-card>
<post-comment-list comments="$ctrl.comments"></post-comment-list>
<post-comment-form on-save-comment="$ctrl.saveComment($event)"></post-comment-form>
post-detail-card.html:
<div class="page-header">
<h1 class="text-xs-center text-uppercase text-justify">
{{$ctrl.post.title}}
</h1>
<p class="text-xs-center text-muted">{{$ctrl.post.createdAt|date:'short'}}</p>
</div>
<div class="card">
<div class="card-block">
<p>
{{$ctrl.post.content}}
</p>
</div>
<div class="card-footer">
back to <a href="#" ui-sref="app.posts">{{'post-list'}}</a>
</div>
</div>
import template from './post-detail-card.html';
let postDetailCardComponent = {
restrict: 'E',
bindings: {
post: '<'
},
template
};
export default postDetailCardComponent;
bindings
here to bind expression, string, or callback methods to the controller of this component.<
means one-way binding, like most of modern
frameworks, such as Angular 2, React etc. It receives the data from its
parent component, but never sync changes to its parent when it is
updated.Lets have a look at CommentList.
post-comment-list.html:
<div class="card" ng-if="$ctrl.comments">
<div class="card-block">
<post-comment-list-item comment="c" ng-repeat="c in $ctrl.comments"></post-comment-list-item>
</div>
</div>
import template from './post-comment-list.html';
import controller from './post-comment-list.controller.js';
let postCommentListComponent = {
restrict: 'E',
bindings: {
comments: '<'
},
template,
controller
};
export default postCommentListComponent;
post-comment-list-item.html:
<div class="media">
<div class="media-left media-top">
<a href="#">
<img class="media-object" src="../" alt="...">
</a>
</div>
<div class="media-body">
<h6 class="media-heading">{{$ctrl.comment.createdBy}} • {{$ctrl.comment.createdAt}}</h6>
<p> {{$ctrl.comment.content}}</p>
</div>
</div>
import template from './post-comment-list-item.html';
let postCommentListItemComponent = {
restrict: 'E',
bindings: {
comment: '<'
},
template
};
export default postCommentListItemComponent;
post-comment-form.html:
<div class="card">
<div class="card-block">
<form id="form" name="form" class="form" ng-submit="$ctrl.saveCommentForm()" novalidate>
<div class="form-group" ng-class="{'has-danger':form.content.$invalid && !form.content.$pristine}">
<!--<label class="form-control-label" for="content">{{'comment-content'}} *</label>-->
<textarea class="form-control" type="content" name="content" id="content" ng-model="$ctrl.newComment.content" rows="8" required
ng-minlength="10">
</textarea>
<div class="form-control-feedback" ng-messages="form.content.$error" ng-if="form.content.$invalid && !form.content.$pristine">
<p ng-message="required">Comment is required</p>
<p ng-message="minlength">At least 10 chars</p>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-lg" ng-disabled="form.$invalid || form.$pending"> {{'save'}}
</button>
</div>
</form>
</div>
</div>
import template from './post-comment-form.html';
import controller from './post-comment-form.controller.js';
let postCommentFormComponent = {
restrict: 'E',
bindings: {
onSaveComment: '&'
},
template,
controller
};
export default postCommentFormComponent;
class PostCommentFormController {
constructor($scope) {
'ngInject';
this._$scope = $scope;
this.newComment = {
content: ''
};
}
saveCommentForm() {
this.onSaveComment({$event: this.newComment})
.then(
(res) => {
this.newComment = {
content: ''
};
this._$scope.form.$setPristine();
}
);
}
}
export default PostCommentFormController;
The comment data can be transfered via a
$event
object.
Through this way, post-comment-form component itself does not handle the
saving event, and just delegates it to post-detail component for
further processing.In the post-detail components, it receives
$event
data and processes it.saveComment(event) {
console.log("saving comment...@");
let deferred = this._$q.defer();
this._Post.saveComment(this.id, event)
.then((res) => {
//refresh comments by post.
console.log('saved comment.');
this._Post.getCommentsByPost(this.id)
.then((res) => {
console.log('refresh comments list...');
this.comments = res;
this._toastr.success('Comment was added!');
deferred.resolve(res);
});
});
return deferred.promise;
}
//...
import postDetailCardComponent from './post-detail-card.component';
import postCommentListComponent from './post-comment-list.component';
import postCommentListItemComponent from './post-comment-list-item.component';
import postCommentFormComponent from './post-comment-form.component';
//...
let postsModule = angular.module('posts', [commonSevices, uiRouter])
//...
.component('postDetailCard', postDetailCardComponent)
.component('postCommentList', postCommentListComponent)
.component('postCommentListItem', postCommentListItemComponent)
.component('postCommentForm', postCommentFormComponent)
//...
评论