Javascript 主干视图未获取JSON对象
制作前端带有主干的Rails应用程序。有两个主干模型,出版物和文章。我在表达我的主要观点时遇到问题。下面是给我带来问题的代码:Javascript 主干视图未获取JSON对象,javascript,ruby-on-rails,json,backbone.js,model-view-controller,Javascript,Ruby On Rails,Json,Backbone.js,Model View Controller,制作前端带有主干的Rails应用程序。有两个主干模型,出版物和文章。我在表达我的主要观点时遇到问题。下面是给我带来问题的代码: SimpleGoogleReader.Views.PublicationsIndex = Backbone.View.extend({ template: JST['publications/index'], el: '#publication', events:{ 'click #new_feed': 'createFeed' },
SimpleGoogleReader.Views.PublicationsIndex = Backbone.View.extend({
template: JST['publications/index'],
el: '#publication',
events:{
'click #new_feed': 'createFeed'
},
initialize: function() {
this.listenTo(this.model, "sync", this.render);
},
render: function(){
this.$el.html( this.template({publications: this.model.toJSON()}) );
return this;
},
createFeed: function(e){
e.preventDefault();
var feed_url = $('#new_feed_name').val();
// var that = this;
this.model.create(
{url: feed_url},
{ success: function(data){
$.post('/articles/force_update', {url: feed_url, publication_id: data.id}, function(data){
});
}
}
);
}
});
我知道正在调用render函数,当我调用console.log(this.model.toJSON())时,它不会返回JSON对象。但是,渲染功能在Articles视图中工作正常:
SimpleGoogleReader.Views.ArticlesIndex = Backbone.View.extend({
template: JST['articles/index'],
el: '#article',
initialize: function() {
this.listenTo(this.model, "sync", this.render);
},
render: function(){
console.log(this.model.toJSON());
this.$el.html( this.template({articles: this.model.toJSON()}) );
return this;
}
});
**编辑**这是我的路由器,我在这里实例化视图:
SimpleGoogleReader.Routers.Publications = Backbone.Router.extend({
routes: {
'': 'home',
'all_articles': 'get_all_articles',
'publications/:id': 'articles_by_id',
'publications/:id/delete': 'delete_publication'
},
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(){
articles.fetch( {success: function(){}} );
});
publications.fetch();
},
有没有关于差异所在的想法?您如何实例化这些视图?请参阅编辑。希望这就是你的意思,但是为什么它只适用于一个视图而不适用于另一个呢?另外,如果我只使用console.log(this.model),我会得到:child{length:0,models:Array[0],_byId:Object,_listenId:“l2”,_events:Object…}其中至少应该有一个发布