Backbone,我应该使用sync还是JQuery? 我可以听吗?

在前端使用Backbone的Rails应用程序。 基本上我希望用户输入一个字符串并点击提交按钮。 单击该按钮后,将更新两个视图。 我从SO用户那里得到了很多帮助,但遇到了另一个问题。 以下是详细信息:

我有两个模型,Publication和Article(带有相应的Rails模型)。 我正在使用gem(Feedzirra)来解析用户输入的RSS URL。 所以我将url发送到/ publications并使用返回的数据我可以获取id,这样我就可以将它用作POST到/ articles的输入。 现在是重新渲染两个视图的时候了。 起初我尝试使用Backbones .sync函数来发出POST请求,并让视图监听更改并相应地更新。 但是,因为我需要来自一个请求的数据,所以我可以将它提供给下一个请求,我必须使用$ .post()而不是.sync。 这主要是因为我不太了解如何使用.sync。 这可以使用.sync吗?

这是Backbone中的Article视图:

SimpleGoogleReader.Views.ArticlesIndex = Backbone.View.extend({ template: JST['articles/index'], el: '#article', events:{ 'click #new_feed': 'createFeed' }, initialize: function() { this.listenTo(this.model, "sync", this.render); }, render: function(){ this.$el.html( this.template({articles: this.model.toJSON()}) ); return this; }, createFeed: function(e){ e.preventDefault(); var feed_url = $('#new_feed_name').val(); $.post('/publications', {url: feed_url}, function(data){ $.post('/articles/force_update', {url: feed_url, publication_id: data.id}, function(data){ var publications = new SimpleGoogleReader.Collections.Publications(); publications.fetch({ success: function(publications){ var view = new SimpleGoogleReader.Views.PublicationsIndex({model: publications}); view.render(); } }); }); }, 'json'); } }); 

这是我在Backbone路由器中的home函数:

  home: function(){ var publications = new SimpleGoogleReader.Collections.Publications(); var articles = new SimpleGoogleReader.Collections.Articles(); var pubIndex = new SimpleGoogleReader.Views.PublicationsIndex({model: publications}); var artIndex = new SimpleGoogleReader.Views.ArticlesIndex({model: articles}); articles.listenTo(publications, "change:shown", function(){ var articles = new SimpleGoogleReader.Collections.Articles(); articles.fetch({ success: function(articles){ var view = new SimpleGoogleReader.Views.ArticlesIndex({model: articles}); view.render(); } }); }); publications.fetch(); articles.fetch(); }, 

我在这里看到的另一件事是,继续使用JQuery嵌套POST请求而不是Backbone的同步。 一旦第二个返回,我重新渲染出版物视图。 然后我只是让文章视图在发布视图中监听更改并在此时更新自己。 从我发现我认为你只能听Backbone的模型变化而不是它的视图变化。 真的吗?

请指教!

首先将以下代码添加到您的两个视图中:

 initialize: function() { this.listenTo(this.model, "sync", this.render); } 

并在路由器中:

 home: function(){ var publications = new SimpleGoogleReader.Collections.Publications(); var articles = new SimpleGoogleReader.Collections.Articles(); var pubIndex = new SimpleGoogleReader.Views.PublicationsIndex({model: publications}); var artIndex = new SimpleGoogleReader.Views.ArticlesIndex({model: articles}); articles.listenTo(publications, "sync", function(){ // no need to create the articles again // var articles = new SimpleGoogleReader.Collections.Articles(); // just fetch the articles, the `this.listenTo(this.model, "sync", this.render);` // will render the view for you articles.fetch(); }); publications.fetch(); }, 

我想你必须在这里调用articles.fetch()

 $.post('/publications', {url: feed_url}, function(data){ $.post('/articles/force_update', {url: feed_url, publication_id: data.id}, function(data){ var publications = new SimpleGoogleReader.Collections.Publications(); publications.fetch({ success: function(publications){ var view = new SimpleGoogleReader.Views.PublicationsIndex({model: publications}); view.render(); // here !!!! } }); }); }, 'json');