javascript Backbone collection.create() 不返回更新后的模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10047465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:33:23  来源:igfitidea点击:

Backbone collection.create() does not return the updated model

javascriptbackbone.js

提问by Jose Armesto

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 lielements and shows the button with "N new Tweets". If you click it, it shows the hidden lielements, 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.

为了学习骨干,我创建了一个类似 Twitter 的应用程序。所以你知道 Twitter 每 N 秒向服务器发送一个 GET 请求来检查新的推文。如果有新推文,它会创建隐藏的li元素并显示带有“N 个新推文”的按钮。如果单击它,它会显示隐藏的li元素,显示新推文。但是当您添加一条新推文时,行为会有所不同:该推文是可见的。您不必单击按钮即可查看它。

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:

我已经为隐藏的推文制作了第一部分。对于发布新推文并直接展示的部分,我认为通过创建新模型、调用 collection.create() 并触发正确的事件来轻松完成,例如:

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);

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 POSTrequest is made to the server, it returns the new created model in json)

然后,我的收藏订阅了事件“posted_new_tweet”,所以每次用户发布一条新推文时,都会调用我收藏的一个特定方法。这种方法运行良好,直到由于触发器中传递的变量 created_comment 而出现错误:它不是“完整的”。我的意思是模型有一些未定义的属性,如“id”或 *“created_on”*。这些属性是在服务器端计算的,但我认为如果我通过wait=true,它会等待并使用服务器给出的响应更新我的模型(当向服务器发出POST请求时,它返回新创建的模型json)

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?

我的模型不应该也有服务器端属性吗?这是处理这种事情的正确方法吗?如果不是,我怎样才能有 2 种不同的方法来显示集合视图?

Thank you!

谢谢!

回答by abraham

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

create即使你通过了,它仍然是异步的{ wait: true }。不同之处在于wait模型不会立即添加到集合中,而wait主干不会将其添加到集合中,直到来自服务器的成功响应。

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

您应该做的是添加一个成功回调,以create在服务器响应时触发事件。

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);
}