ember.js - Ember — observe for creation/deletion of records -


मान लें कि मेरे पास 2 स्वतंत्र नियंत्रक / मॉडल हैं: डॉग और Cat < / Code>।

अब, जब भी मैं नया cat रिकॉर्ड बना देता हूं (या मौजूदा एक को हटा देता हूं) मुझे डॉग नियंत्रक को बनाने / हटाने के लिए निरीक्षण करना चाहते हैं रिकॉर्ड और जब नया कैट रिकॉर्ड बनाया गया है, मैं इसे कुछ कार्रवाई करने के लिए चाहता हूं (उदाहरण के लिए console.log ('bark!') )।

मैं इस मामले में सीधे बिल्ली नियंत्रक से डॉग नियंत्रक से कार्रवाई नहीं भेजना चाहता हूं; मुझे कुत्ते नियंत्रक खुद के लिए जिम्मेदार होना चाहते हैं। यदि यह संभव है, तो क्या कुत्ते नियंत्रक को यह बताने का कोई तरीका है कि क्या रिकॉर्ड बनाया गया या हटाया गया?

कोई भी विचार कैसे करें?

एम्बर डेटा का उपयोग

कुत्ते / बिल्ली नियंत्रक पर आप के लिए एक संदर्भ बना सकते हैं बिल्ली / कुत्ते का संग्रह।

मान लें कि आप कुत्ता नियंत्रक बना रहे हैं

  App.DogRoute = Em.Route.extend ({model: function ()} .store.find ('dog');}, setupController: फ़ंक्शन (नियंत्रक, मॉडल) {this._super (नियंत्रक, मॉडल); नियंत्रक.सेट ('बिल्लियों', this.store.all ('बिल्ली')); }}); App.DogController = Em.ArrayController.extend ({catsChanged: function () {} .observes ('cats')});  

इसके अतिरिक्त आप एक सरणी पर्यवेक्षक को जोड़ सकते हैं और देखें कि आइटम कैसे बदल रहे हैं।

  App.DogRoute = Em.Route.extend ({मॉडल: function () {Return.store.find ('dog');}, setupController: फ़ंक्शन (नियंत्रक, मॉडल) {this._super (नियंत्रक, मॉडल); var सब = this.store.all ('बिल्ली'); नियंत्रक .set ('बिल्लियों', सभी); all.addArrayObserver (नियंत्रक);}}); App.DogController = Em.ArrayController.extend ({arrayWillChange: function (array, start, removecount, addCount) {console.log (आर्गेंगमेंट);}, arrayDidChange: function (array, start, removeCount, addCount) {console.log ( तर्क);}});  

ज़रूरतों का इस्तेमाल करना और नियंत्रक को देखना

  ऐप.कैटेंसींट्रोलर = एम। अर्रेकंट्रोलर.एंडेंड ({की जरूरत है: 'कुत्ता', कुत्तों: एमओएमपीपीटेड उपनाम ('controllers.dogs'), कुत्तों परिवर्तित: कार्य () {console.log ('कुत्तों को बदल दिया');} .observes ('कुत्तों। []')}); App.DogsController = Em.ArrayController.extend ({needs: 'cat', cats: em.computed.alias ('controllers.cats'), catsChanged: function () {console.log ('cats changed');} देखता है ('बिल्लियों। []')});  

Comments

Popular posts from this blog

c++ - C/pp Sockets, recv()/send() works only under gdb -

GO: Serve static pages -

objective c - How to open front/back camera at the same time in iOS developing? -