骨干collection.create()不返回更新后的模型 [英] Backbone collection.create() does not return the updated model

查看:102
本文介绍了骨干collection.create()不返回更新后的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要学习骨干进出口创造一个类似Twitter的应用程序。所以,你知道Twitter发送GET请求到服务器每隔N秒,以检查是否有新的鸣叫。如果有新的鸣叫,它会创建隐藏的的元素,并显示了N新推文的按钮。如果你点击它,它显示了隐藏的的元素,呈现出新的鸣叫。
但该行为是不同的,当你添加一个新的tweet:鸣叫是可见的。你不必点击该按钮才能看到它。

To learn backbone Im creating a Twitter like app. So you know that Twitter sends a GET request to the server every N seconds to check for new tweets. If there are new tweets, it creates the hidden li elements and shows the button with "N new Tweets". If you click it, it shows the hidden li elements, showing the new tweets. But the behaviour is different when you add a new tweet: the tweet is visible. You don't have to click the button to see it.

我已经取得了第一部分,为隐藏鸣叫。对于发布新的鸣叫,并将其显示照片直接的一部分,我认为这将是很容易通过创建新模式,调用collection.create(),并触发正确的事件做,是这样的:

I already have made the first part, for the hidden tweets. For the part of posting a new tweet and showing it direclty, I thought it would be easy to do by creating the new model, calling collection.create() and triggering the right event, something like:

var newTweet = new Tweet();
newTweet.set( /* set the attributes here. Some attributes are missing, because they are calculated server side */ );

var created_tweet = this.collection.create( newTweet, { silent: true, wait: true } ); // I choose silent=true because the add event on my collection is in charge of adding the new hidden tweets when there are new ones on the server
this.collection.trigger("posted_new_tweet", created_tweet);

然后,我的收藏​​订阅事件posted_new_tweet,所以用户发布一个新的鸣叫,我收藏的具体方法被调用每次。
这种方法工作正常,直到我由于触发传递的变量created_comment错误:它不是完全。我的意思是,该模型具有像一些属性的身份证的或*created_on*这是不确定的。这些属性计算服务器端,但我想,如果我通过的等待= TRUE 后,它会等待并更新我的服务器给出的响应模型(当发表请求向服务器发出,则返回JSON的新创建的模型)

Then, my collection is subscribed to the event "posted_new_tweet", so every time a user posts a new tweet, a specific method of my collection is being called. This approach was working fine until I got errors due to the variable created_comment passed in the trigger: it is not "complete". I mean that the model has some attributes like "id" or *"created_on"* that are undefined. These attributes are calculated server side, but I thought that if I passed wait=true, it would wait and update my model with the response given by the server (when a POST request is made to the server, it returns the new created model in json)

应该不是我的模型有服务器端藏汉属性?难道这样的事情是正确的做法?如果它不是,我怎么能有2个不同的方法来显示一个集合视图?

Shouldn't my model have the server side attributes aswell? Is it the right approach for such a thing? In case that it is not, how can I have 2 different methods to display a collection view?

感谢您!

推荐答案

创建仍是异步的,即使你通过 {等待:真正} 。所不同的是没有模型将添加到收藏立即同时与骨干不会将其添加到集合,直到从服务器成功响应

create is still asynchronous even if you pass { wait: true }. The difference is without wait the model will get added to the collection immediately while with wait backbone won't add it to the collection until a success response from the server.

你应该做的是成功的回调添加到创建将触发对服务器响应事件。

What you should do is add a success callback to create that fires the event upon the server response.

var created_tweet = this.collection.create( newTweet, { silent: true, wait: true, success: this.successCallback } );

// Add successCallback as a method to the collection
successCallback: function(collection, response) {
  // I'm not 100% positive which values are passed to this function. Collection may actually be the new model.
  this.collection.trigger("posted_new_tweet", created_tweet);
}

这篇关于骨干collection.create()不返回更新后的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆