2012-05-11 36 views
0

我寫了下面的代碼(*)Backbone.model:對象功能(一){返回新N(A)}有沒有方法 '有'

當我試圖運行下面的代碼(** )在我的JS控制檯,
我得到以下結果:

"your attributes are: ", Object // json object taken from the server as I was expecting 

Object function (a){return new n(a)} has no method 'has' 

爲什麼我得到has no method 'has'的問題?

-

(**)

require.config({ 
    baseUrl: "/" 
}); 

require(["js/models/task"], function (Model) { 
    var model = new Model({id: 1}); 
    model.fetch(); 
    console.log(model.attributes); 
}); 

(*)

define([], function() { 
    var MyModel = Backbone.Model.extend({ 

     initialize: function() 
     { 
      this.bind("change", function() { 
       console.log("this model has been changed") 
      }); 

      this.bind("error", function (model, error) { 
       console.log(error); 
      }) 
     }, 

     urlRoot: "/", 
     url: function() { 
      var base = this.urlRoot || (this.collection && this.collection.url) || "/"; 
      if (this.isNew()) return base; 
      return base + this.id; 
     }, 

     validate: function (attribute) { 
      if (typeof attribute === "object") { 
       console.log("your attributes are: ", attribute); 
      } 
     } 

    }); 

    return MyModel; 
}); 
+2

它只是今天happend給我,將Underscore更新爲最新版本修復程序d it。 – fguillen

+0

是的,這只是一個下劃線版本的問題。 – underscore666

回答

0

fetch是異步的請嘗試以下操作:

require(["js/models/task"], function (Model) { 
    var model = new Model({id: 1}); 
    model.fetch({success: function() { 
     console.log(model.attributes); 
    }}); 

}); 
+0

如果我只在你的代碼中註釋'console.log(model.attributes);',我會得到同樣的錯誤。所以問題就在於我進行調用'model.fetch();' – underscore666

+1

好 - 你有下劃線嗎?這是骨幹 – timDunham

+1

的依賴關係當然,也許這可能是一個版本問題。 _.VERSION - > 1.3.0; Backbone.VERSION 「0.9.2」 – underscore666

相關問題