骨干视图问题

时间:2012-08-13 11:04:28

标签: javascript jquery backbone.js

我在视图中创建了一个blockView实例,并将模型作为参数传递。但是当我运行它时,它给出了错误“TypeError:无法将undefined转换为object”。我试图追踪这个错误,但我无法得到。任何人。 Plz找到下面的代码

 var DCSSPACE = DCSSPACE || {};
    $(document).ready(function(){
        WorkSpace = new draw2d.Workflow( "canvasdiv" );
        collection = new DCSSPACE.collection.collection();
        view = new DCSSPACE.view.view({WorkSpace : WorkSpace, collection : collection});


    });

    DCSSPACE.view = function(){
        var uniqueid = 1, blockCounter = 0;
    var view = Backbone.View.extend({
        el : $("#canvasdiv"),
        views : {},

        initialize : function() {
            console.log("View");

            this.collection.bind("add", function(m, temp, data){
                console.log(data.evt);
//Here i am getting error "TypeError: can't convert undefined to object"                
this.views[m.cid] = new blockView({data : data, model : m, id : "view_" + m.cid});
                },this);

            this.collection.bind("remove", function(m){
                this.views[m.cid].remove();
                delete this.views[m.cid];
            }, this);
            this.render(WorkSpace);
        },
        render : function(WorkSpace) {
            $("#sidediv").accordion();
            this.disableSelection(document.body);
            var menu = new draw2d.FlowMenu(WorkSpace);
            WorkSpace.addSelectionListener(menu);
            var toolwindow = new draw2d.Toolbox();
            WorkSpace.setToolWindow(toolwindow);
            toolwindow.setPosition(0,0);
            $(".btn").draggable({
                helper : "clone",
                stop : function(event, ui) {
                    var data = {
                        id : this.id,
                        evt : event.originalEvent,
                    };
                    if(event.originalEvent.pageX >= 220) {
                        var m = new DCSSPACE.model.FunctionBlockModel();
                        collection.add(m, data);
                    }
                }
            });

            return this;
        },
        /* Function for deselect body contents */
        disableSelection : function(target) {
            if( typeof target.onselectstart != "undefined")//IE
                target.onselectstart = function() {
                    return false;
                }
            else if( typeof target.style.MozUserSelect != "undefined")//Firefox
                target.style.MozUserSelect = "none"
            else//All other
                target.onmousedown = function() {
                    return false
                }
            target.style.cursor = "default"
        }
    });

    var blockView = Backbone.View.extend({
            initialize : function(formalObj){
                console.log("block view");
                $("#canvasdiv").mousemove(this, this.mousemove).mouseup(this, this.mouseup);
                this.model.set({id : formalObj.data.id});
                console.log(JSON.stringify(collection));
                var nodeObj = this.block(formalObj.data);

            },

            block : function(data){
                var Obj = new draw2d.node();
                var self = this;
                Obj.onDoubleClick = function(){
                    self.informationBlock(Obj.getnodeName());
                };
                    Obj.setTitle(collection.get(data.id).get("id")+" "+ (uniqueid++));
                    Obj.setFooter("#" + (++blockCounter));
                    WorkSpace.addFigure(Obj, data.evt.pageX - 205, data.evt.pageY);

    });
    return{
        view : view,
        blockView : blockView
    }

    }();

1 个答案:

答案 0 :(得分:1)

尝试像这样实例化您的视图:

view = new DCSSPACE.view.view({model : WorkSpace, collection : collection});

在您的View实施中,替换WorkSpace的所有this.model引用。

个人笔记:我认为你应该努力清理和整理你的代码:) Personal note2 :当您收到JS错误时,通常会有更多信息可以帮助您找到问题,例如,确切的代码行。