0

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

 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
    }

    }();
4

1 回答 1

1

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

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

WorkSpace并在您的 View 实现中替换this.model.

个人说明:我认为您应该努力清理和组织您的代码 :)
个人说明2:当您收到 JS 错误时,通常会有更多信息有助于找到问题,例如,确切的代码行。

于 2012-08-13T11:25:07.743 回答