骨干呼叫模型功能
Backbone call model function
我有一个集合,从这个集合中我选择了一个模型。在这个模型中,我之前定义了一个函数。如何调用从集合中选择的模型的这个函数?
我调用模型函数(但未定义):
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;
},
似乎您的集合应该指定模型类型。查看文档获取更多信息
更新:来源:
var Model = Backbone.Model = function(attributes, options) {
var attrs = attributes || {};
options || (options = {});
this.cid = _.uniqueId('c');
this.attributes = {};
if (options.collection) this.collection = options.collection;
if (options.parse) attrs = this.parse(attrs, options) || {};
attrs = _.defaults({}, attrs, _.result(this, 'defaults'));
this.set(attrs, options);
this.changed = {};
this.initialize.apply(this, arguments);
};
要将您的模型关联到一个集合-该集合不是由集合通过fetch创建的-只需在创建它时将集合传递给模型(假设是这样)。Collection是实际的Collection实例):
new Model({collection: this.collection})
相关文章:
- 在VanillaJS中模拟模型双向数据绑定
- 添加文字和评论功能更新Div
- JavaScript打印功能使日历停止工作
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何防止网页加载后自动启动功能
- 除修剪外的其他功能
- 悬停功能触发器
- 为集合分配大量的模型弹药
- 如何使用backbone.js从集合中获取模型名称
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 视图模型中的功能范围
- 向骨干模型添加更多功能
- 在Voodoo.js中,什么事件触发了模型的“更新”功能
- 如何使用ng模型实现功能绑定(使用plnkr)
- 为什么模型名称更改时功能不起作用
- 击倒视图模型的多个功能
- E2e测试与量角器-如何返回选定的菜单项从功能内页模型
- Knockout -具有多个模型的foreach循环中的功能
- 骨干呼叫模型功能
- jQuery ui's的调整大小功能无法与chrome中的flexbox模型正确交互,但在FF和IE中取得了成功