BackboneJS如何在模块之间传递值

时间:2014-09-18 11:49:34

标签: javascript jquery backbone.js

我想根据用户可以点击字母表的字母显示艺术家列表。但不知何故,即使这封信被通过,我也会收到错误,例如尽管我的console.log给了我正确的字母,但它说这封信是不确定的。这是我做的事情:

我的artistsAll.js模块:

var ArtistsAll = App.module();
var ArtistsAllView = Backbone.View.extend({
  tagName : 'li',
  template: 'artistItem',
  serialize: function() {
    return this.model.toJSON();
  }
});

ArtistsAll.View = Backbone.View.extend({
  tagName  : 'ul',
  className : 'artistList',
  initialize: function() {
    this.collection.on('sync', _.bind(this.render, this));          
  },
  beforeRender: function() {
    var self = this;    
    this.collection.each(function(item) {
        self.insertView(new ArtistsAllView({model: item}))
    })
  }     
});

ArtistsAll.ArtistsAllCollection = Backbone.Collection.extend({
  url: function() {
    return App.APIO + '/i/search/artist?name=' + this.letter; 
  }
});

return ArtistsAll;

基本上,每个字母都有一个端点,例如/i/search/artist?name=b

然后,在我的控制器中,我有:

var ArtistsAllModule = require('modules/artistsAll');

ArtistController.prototype.initArtistLetter = function(letter) {
   this.artistsAllCollection = new ArtistsAllModule.ArtistsAllCollection();
   this.artistsAllCollection.letter = letter;
   App.useLayout('artistLetter', 'artistLetter').setViews({
      '.artistsDiv': new ArtistsAllModule.View({collection: this.artistsAllCollection});
   }).render();     
   this.artistsAllCollection.fetch();
};  

我得到的只是空白页,没有错误....有人可以告诉我这里的问题是什么吗?

1 个答案:

答案 0 :(得分:0)

尝试修改您的代码,如下所示:

ArtistsAll.View = Backbone.View.extend({
  tagName  : 'ul',
  className : 'artistList',
  initialize: function() {
    // here's the modification
    this.listenTo(this.collection, 'sync', this.render, this); // but where's your render function!
  },
相关问题