[英]Backbone embedded collection event
I've created a model (see Delegation) made of basic attributes (name, activity) and one collection (members). 我创建了一个由基本属性(名称,活动)和一个集合(成员)组成的模型(请参阅委派)。
The fetch method update the model and trigger a sync event on it - but I would like to be notified when the embedded collection is synced (ie in a real case to render a view when the inner collection is synced). fetch方法会更新模型并在其上触发同步事件-但是我想在嵌入式集合被同步时得到通知(即,在实际情况下,当内部集合被同步时呈现视图)。
I tried this 我试过了
this.listenTo(this.get('members'),'sync',function(){...}
but the corresponding event never fires. 但是相应的事件永远不会触发。
What is the proper way to have it triggered? 触发它的正确方法是什么?
When you execute the
fetch
function of Backbone , it returns a PROMISE , you can use the function then to launch a function at the moment thefetch
is finished, you can also use thecatch
for when it fails.当您执行Backbone的
fetch
功能时,它将返回一个PROMISE ,您可以使用该功能,然后在fetch
完成时启动一个功能,也可以在失败时使用catch
。
this.model = new Backbone.Model()
this.model.url ="example"
this.model.fetch().then((data) => {
console.log("succes: " + data); // Finished the fetch successfully
}).catch((data) =>{
console.log("error: " + data); // The fetch ended in error
})
You can use it in this part Backbone.Model.prototype.fetch or if your fetch function returns a Promise, use it when calling the fetch of the model. 您可以在此部分Backbone.Model.prototype.fetch中使用它,或者如果您的提取函数返回Promise,则在调用模型的提取时使用它。
I don't see any code that fetches the collection, hence sync
is not triggered on the collection. 我看不到任何可获取集合的代码,因此不会在集合上触发
sync
。 You should do delegation1.get('members').fetch()
. 你应该做的
delegation1.get('members').fetch()
。 For this to work, the collection should have it's own URL as well. 为了使它起作用,集合还应该具有其自己的URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.