Javascript 主干网,我应该使用sync还是JQuery?我能听一听吗?
Rails应用程序在前端使用主干。基本上我希望用户输入一个字符串并点击提交按钮。单击该按钮后,将更新两个视图。我从SO用户那里得到了一些很大的帮助,但遇到了另一个障碍。详情如下: 我有两个模型,出版物和文章以及相应的Rails模型。我使用gem Feedzirra解析用户输入的RSS URL。因此,我将url发送到/publications,并使用返回的数据获取id,以便将其用作发布到/articles的输入。现在是重新渲染这两个视图的时候了。起初,我尝试使用Backbones.sync函数发出POST请求,让视图侦听更改并相应地更新。但是,由于我需要一个请求中的数据以便将其提供给下一个请求,因此我必须使用$.post而不是.sync。这主要是因为我不知道如何使用.sync。使用.sync可以这样做吗 以下是主干网中的文章视图:Javascript 主干网,我应该使用sync还是JQuery?我能听一听吗?,javascript,jquery,ruby-on-rails,post,backbone.js,Javascript,Jquery,Ruby On Rails,Post,Backbone.js,Rails应用程序在前端使用主干。基本上我希望用户输入一个字符串并点击提交按钮。单击该按钮后,将更新两个视图。我从SO用户那里得到了一些很大的帮助,但遇到了另一个障碍。详情如下: 我有两个模型,出版物和文章以及相应的Rails模型。我使用gem Feedzirra解析用户输入的RSS URL。因此,我将url发送到/publications,并使用返回的数据获取id,以便将其用作发布到/articles的输入。现在是重新渲染这两个视图的时候了。起初,我尝试使用Backbones.sync函数发出
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');
}
});
以下是主干路由器中的my 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请求。一旦第二个返回,我将重新呈现publications视图。然后我只需要让文章视图倾听出版物视图中的变化,并在这一点上进行自我更新。根据我的发现,我认为您只能监听主干的模型更改,而不能监听视图更改。这是真的吗
请告知 首先将以下代码添加到两个视图中:
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;执行?尝试在控制台中记录某些内容当页面最初加载时,将执行yes。但当我点击提交按钮开始整个发布过程时就不是了。
$.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');