2012-06-29 19 views
6

में अपरिभाषित की संपत्ति 'idAttribute' नहीं पढ़ सकता है मेरे पास दो मॉडल (उपयोगकर्ता और कार्य) हैं जो Backbone.RelationalModel के उदाहरण हैं।_.bindAll (यह) और Uncaught TypeError: रीढ़ की हड्डी-relation.js

इन दो मॉडलों के बारे में संबंध है निम्नलिखित:

// Task model 

    var Task = Backbone.RelationalModel.extend({ 

     relations: [ 
      { 
       type: 'HasOne', 
       key: 'user', 
       relatedModel: User 
      } 
     ], 

     urlRoot: 'someUrl' 

    }); 

तब मेरे पास एक संग्रह जो कोड इस तरह दिखता है:

var FollowerCollection = Backbone.Collection.extend({ 
    initialize: function() { 
     _.bindAll(this); 
    } 
    model: User 
}); 


var User = Backbone.RelationalModel.extend({ 

}); 

जब मैं एक FollowerCollection मैं पर लाने बनाने निम्न त्रुटि प्राप्त करें:

Uncaught TypeError: Cannot read property 'idAttribute' of undefined 

backbone-relation version 0.5.0


यहाँ रीढ़-relation.js के कोड का एक टुकड़ा

if (!(model instanceof Backbone.Model)) { 
    // Try to find 'model' in Backbone.store. If it already exists, set the new properties on it. 
     var existingModel = Backbone.Relational.store.find(this.model, model[ this.model.prototype.idAttribute ]); 

समस्या _.bindAll(this) से संबंधित है की रीढ़ की हड्डी-relation.js की लाइन 1565 को क्योंकि अगर मैं इसे टिप्पणी करता हूं, यह ठीक से काम करता है।
क्यों? कोई विचार?


+0

क्या आप सुनिश्चित हैं कि 'उपयोगकर्ता' मॉडल परिभाषित करता है जब आप संबंध घोषित करते हैं? –

+0

@ mashingan मैंने टास्क मॉडल के बारे में सभी कोड संलग्न किए हैं। क्या आप एक नज़र देख सकते हैं? धन्यवाद। –

+0

आप FollowerCollection और उपयोगकर्ता के लिए कोड कैसे संलग्न करते हैं ताकि हम समस्या को खोजने का प्रयास भी कर सकें? वर्तमान में मुझे दो वर्गों के बीच कोई संबंध नहीं दिखता है। – jakee

उत्तर

3

_.bindAll को हटाकर काम करता है।

यह एक शर्म की बात है, क्योंकि यह वास्तव में एक आसान काम है। इसे बैकबोन के कुछ हिस्सों से बुरी तरह से बातचीत करनी चाहिए। मैं v9.10

मैं इस विधि का हर समय उपयोग करता हूं, और समस्याएं कभी-कभी आती हैं (जैसे कि जब आप संग्रह में थोक जोड़ना चाहते हैं)।

मेरे लिए, समस्या यह Backbone.js विधि में था:

// Get a model from the set by id. 
get: function(obj) { 
    if (obj == null) return void 0; 
    this._idAttr || (this._idAttr = this.model.prototype.idAttribute); 
    return this._byId[obj.id || obj.cid || obj[this._idAttr] || obj]; 
}, 

कोड this.model.prototype में विफल रहता है क्योंकि प्रोटोटाइप अनिर्धारित रहता है। क्या? हां। असली के लिए।

समस्या यह है कि जब _.bindAll कहा जाता है, तो यह संग्रह के सभी गुणों को बांधता है, जैसा कि @ जेकी कहते हैं। ऐसा लगता है कि Collection.model, जो मुझे लगता है कि एक बग है।

समाधान यह तय होने तक व्यक्तिगत तरीकों को बांधना है।

गीथूब पर एक मौजूदा, लेकिन बंद मुद्दा है: https://github.com/documentcloud/backbone/issues/2080 ऐसा लगता है कि मौजूदा रखरखाव करने वालों को विधि पसंद नहीं है, लेकिन मुझे समझ में नहीं आता क्यों।

0

मेरी परियोजना की तरह वास्तव में बड़ा है मुझे अपना कस्टम बाइंड बनाना था। यहां आपके पास कोड है, यह सबसे पुराने संस्करणों के साथ काम करता है। मैं इस "उदाहरण" के सभी गुणों को बांधता हूं, जिनके पास गुणों के साथ प्रोटोटाइप है।एक संग्रह में मॉडल

https://gist.github.com/patrixd/8025952

//bindAll from underscore that allows 1 argument to bind all the functions from the prototype, 
//or if there are more arguments they will be the only binded 
_.originalBindAll = _.bindAll; 
_.bindAll = function (that) { 
     var funcs = Array.prototype.slice.call(arguments, 1), 
      validKeys = [], fn; 
     if (funcs.length == 0) { 
     for (var i in that) { 
      fn = that[i]; 
      if (fn && typeof fn == "function" && (!fn.prototype ||   
       _.keys(fn.prototype).length == 0)) 
       validKeys.push(i); 
    } 
    _.originalBindAll.apply(_, [that].concat(validKeys)); 
} 
else 
    _.originalBindAll.apply(_, arguments); 

};

संबंधित मुद्दे