2012-08-13 149 views
0

我在視圖中創建blockView的實例並將模型作爲參數傳遞。但是當我運行它會給我錯誤「TypeError:不能將undefined轉換爲對象」。我試圖追查這個錯誤,但我無法得到。任何人plz。 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

嘗試實例你這樣的觀點:

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

而且在你的視圖實現替換爲this.model任何WorkSpace參考。

個人筆記:我想你應該在嘗試工作,清理和組織代碼:)
個人注2:當您收到一個JS錯誤,平時也可幫助查找更多信息這個問題,例如,正好是代碼行。