RequireJS + BackboneJS - 连接模块

时间:2012-12-23 13:02:55

标签: backbone.js requirejs

当我尝试连接包含两个不同BackboneJS视图的模块时,我遇到了问题。

在RequireJS的这个模块中包含一个骨干视图。 我需要在这个模块中,参考另一个模块中的骨干的另一个视图。 我修改了代码以使其更简单

define (["jquery","backbone","collections/controlSearchCollection","views/currentPlaylist","jqueryUI"], function($, Backbone, controlSearchMusic, currentPlaylist){

var searchModule = Backbone.View.extend({
    el: "#containerSearch",

    initialize: function () {
        this.collection = new controlSearchMusic();
    },
    events:{
        "dblclick li":"select"
    },
    select: function(element){
        var trackJSON ={};
        trackJSON["id"]= "playCloud_"+$(element.target).attr("id");
        currentPlaylist.collection.add(trackJSON);   <--- Here is where this error

    }
});

return searchModule;
});

这里,我的另一个RequireJS模块包含了backboneJS的视图。我需要从第一个模块添加到JSON。

define (["jquery","backbone","collections/controlCurrentPlaylistCollection","jqueryUI"], function($, Backbone, controlCurrentPlaylist){

    var currentPlaylist = Backbone.View.extend({
        el: "#currentPlaylist",

        initialize: function(){
            this.collection = new controlCurrentPlaylist();
            this.collection.on("add", this.executeFunction, this);

        },
        executeFunction: function(song){
            alert(song.toSource());
        }
    });
    return currentPlaylist;
});

这两个模块在app.js中实例化

有人可以帮帮我吗?谢谢你!

1 个答案:

答案 0 :(得分:0)

没有确切的错误很难说,但看起来你没有创建currentPlaylist的实例。因此currentPlaylist.collection未定义,并且当您尝试在其上调用add函数时会导致错误。

你可以试试这个:

var cpl = new currentPlaylist(); // create instance
cpl.collection.add(trackJSON);  // cpl.collection should be initialized here