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

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

问题描述

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

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

var newTweet = new Tweet();newTweet.set(/* 在这里设置属性.缺少一些属性,因为它们是服务器端计算的 */);var created_tweet = this.collection.create( newTweet, { silent: true, wait: true } );//我选择silent=true是因为我的集合上的add事件负责在服务器上有新推文时添加新的隐藏推文this.collection.trigger("posted_new_tweet", created_tweet);

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

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

谢谢!

解决方案

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

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

var created_tweet = this.collection.create( newTweet, { silent: true, wait: true, success: this.successCallback } );//将successCallback 作为方法添加到集合中成功回调:函数(集合,响应){//我不是 100% 肯定哪些值被传递给这个函数.收藏实际上可能是新模式.this.collection.trigger("posted_new_tweet", created_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.

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

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)

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?

Thank you!

解决方案

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

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

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