gpt4 book ai didi

backbone.js - 主干 : how can I reference a Collection from a Model

转载 作者:行者123 更新时间:2023-12-04 19:14:08 24 4
gpt4 key购买 nike

我知道它通常以相反的方式完成,但在我的特定用例中,我需要一个模型 发帖时间 引用一个集合(另一个模型 帖子 )。现在我正在尝试:

var Posts = Backbone.Collection.extend({
model: Post
});
var posts = new Posts(); // my collection



var PostsTimes = Backbone.Model.extend({
url: "api/times.php",
initialize: function(options){
this.posts = options.posts;
},
test: function(){
console.log(this.posts);
}
});

var poststimes = new PostsTimes({posts: posts});

但是 this.postsPostsTimes永远停留 undefined .有什么办法可以在不定义全局变量的情况下做到这一点?

最佳答案

你所拥有的结构是正确的,所以它会记录的原因 undefined是因为this在您的 test函数不是模型的实例。

所以你的 PostsTimes会成为

var PostsTimes = Backbone.Model.extend({
url: "api/times.php",
initialize: function(options){
_.bindAll(this);
this.posts = options.posts;
},
test: function(){
console.log(this.posts);
}
});

关于backbone.js - 主干 : how can I reference a Collection from a Model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11831490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com