【发布时间】:2014-05-29 16:35:19
【问题描述】:
我有一个系列,我从这个系列中选择了一个模型。在这个模型中,我之前定义了一个函数。如何调用从集合中选择的模型的这个函数?
我在哪里调用模型函数(但未定义):
var SingleHomePostView = Backbone.View.extend({
tagName: "li",
template: Handlebars.compile(template),
events: {
"click #retweet": "retweet",
},
initialize: function () {
// console.log(this.model);
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render: function (eventName) {
var ad = this.model.toJSON();
ad.cid = this.model.cid;
$(this.el).html(this.template(ad));
return this;
},
retweet: function () {//Here----------
console.log(this.model);// is well defined
console.log("retweet");
console.log(this.model.reTweet()); //here I try to call model function
},
});
这是模型:
var Tweet = Backbone.Model.extend({
reTweet: function(){
var params = {
user_id: 11265832,
//screen_name:"brad"
page:1,
count:2,
};
cb.__call(
"statuses_userTimeline",
params,
function (reply) {
//gestire rate limit error 429
console.log(reply);
// return reply;
}
);
}
});
这是调用先前视图的主视图。它迭代集合并将模型传递给视图。
loadResults: function (eventName) {
//this.showSpinner();
console.log("homepostview"+this.page);
this.isLoading === true;
//console.log(this.IntegratedCollection);
_.each(this.IntegratedCollection.last(this.IntegratedCollection.length).reverse(), function(model){
$(this.el).append(new SingleHomePostView({
model: model
}).render().el);
},this);
this.isLoading === false;;
return this;
},
【问题讨论】:
-
此视图接受来自另一个视图的模型,该模型迭代集合。如果我的 console.log(this.model) 定义明确。如果我创建一个 var=new Tweet(); tweet.reTweet(),工作正常。但我不想创建另一个模型。@clintronx
-
console.log(this.model.reTweet)从视图中打印出什么? -
你能添加视图的创建和你设置模型的位置吗?
-
看起来您的 Collection 可能没有创建 Tweet 类型的模型,而只是将响应解析为基本的主干模型。请也提供您的收藏。
-
我添加了集合迭代并传递给视图。 @clintronx