这是一个 Backbone 帖子模型的示例:
var Post = Backbone.AssociatedModel.extend({
urlRoot: ApiService.resolveRESTUrl('posts'),
defaults: {
age : 0,
fname : "",
lname : "",
manager : null
},
relations:[
{
type:Backbone.One,
key:'User',
relatedModel: function(){ return $injector.get('UserModel') },
collectionType: function(){return $injector.get('UserModel').Collection; }
},
{
type:Backbone.Many,
key:'Last3Comments',
relatedModel:function(){ return $injector.get('CommentModel') },
collectionType:function(){ return $injector.get('CommentModel').Collection },
}
],
getTimeAgo:function() {},
getPicture:function(size){
return this.get('picture_url') ?
ApiService.getImageResizeUrl(this.get('picture_url'),'w'+size+'xh'+size) :
null;
},
});
当帖子加载时,以及帖子数组中是否有使用 Angular 观察器的库(数据用“=”绑定(bind)) 然后我收到以下错误
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! Watchers fired in the last 5 iterations: []
还有
RangeError: Maximum call stack size exceeded at Array.toString (native)
我正在看这样的帖子:
$scope.posts=[];
$scope.$watch('posts', function(model) {
console.log($scope.posts, model);
},true);
$scope.loadPosts=function(){
if($scope.loading || $scope.disabled) return;
$scope.loading=true;
ApiService.request("posts/home",{page:$scope.page,limit:10}).success(function(data){
$scope.loading=false;
$scope.page++;
if(data.error){
alert('Error');
} else {
if(data.data.posts.length==0)
$scope.disabled=true;
$.each(data.data.posts,function(i,e){
var post=new PostModel(e);
$scope.posts.push(post);
});
}
}).error(function(){
alert('Error');
$scope.loading=false;
});
};
有人有什么想法吗?
最佳答案
我解决了我的问题,使用 Angular 的 watchCollection 而不是仅仅观看,因为 watchCollection 用于观看数组。
关于javascript - Backbone 模型可以与 Angular 一起使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26354332/