0

当我尝试连接两个包含两个不同 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 模块包含一个主干JS 的视图。我需要从第一个模块添加到 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 中实例化

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

4

1 回答 1

0

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

你可以试试这个:

var cpl = new currentPlaylist(); // create instance
cpl.collection.add(trackJSON);  // cpl.collection should be initialized here
于 2012-12-23T13:10:26.637 回答