2012-07-18 7 views
7

के बराबर होते हैं, मेरे पास init फ़ंक्शन में सेटअप-कोड के साथ Ember.Controller है। हकीकत में यह कोड AJAX अनुरोध करता है। लेकिन जब मैं इस नियंत्रक के दो उदाहरण बनाते हैं, तो वे हमेशा बराबर होते हैं। क्यों, और मैं यह फिर से क्या कर सकता हूं?इनिट फ़ंक्शन में सेटअप के साथ नियंत्रक हमेशा

मैंने यह सरल उदाहरण बनाया है, जो कंसोल में Test 1Test 2 लिखते हैं। इसके लेखन Test 2 दो बार लिखें।

App = Em.Application.create({}); 

App.TestController = Em.Controller.extend({ 
    content: Em.Object.create({ 
     info: null, 
    }), 
    init: function() { 
     if(this.id == 1) 
     { 
      this.content.set('info', "Test 1"); 
     } 

     if(this.id == 2) 
     { 
      this.content.set('info', "Test 2"); 
     } 
    }, 
}); 

var c1 = App.TestController.create({id: 1}); 
var c2 = App.TestController.create({id: 2}); 

console.log('C1: ' + c1.get('content').get('info')); 
console.log('C2: ' + c2.get('content').get('info')); 


​ 

उत्तर

18

आप अन्यथा, वर्ग घोषणा समय पर सेट मूल्य सभी उदाहरणों द्वारा साझा किया जाएगा init में content मान सेट करने के लिए है,।

App.TestController = Em.Controller.extend({ 
    content: null, 

    init: function() { 
    this._super(); 
    this.set('content', Em.Object.create({ 
     info: null 
    })); 

    // other setup here... 
    } 
}); 

http://codebrief.com/2012/03/eight-ember-dot-js-gotchas-with-workarounds/

+0

हम्म देखें, मैं देख रहा हूँ। लेकिन क्या यह एक बग है और तय किया जाएगा, या यह विचार है? यदि यह एक शर्त है, तो क्या कारण है? – Lux

+1

यह जावास्क्रिप्ट/एम्बर के ऑब्जेक्ट मॉडल का हिस्सा है। Https://github.com/emberjs/ember.js/issues/462 देखें। शायद कभी नहीं बदलेगा ... –

+5

अपने कस्टम 'init' फ़ंक्शन के अंदर 'this._super();' को कॉल करना न भूलें ... – pangratz

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