Properties of the dynamic model Ember.js

Ember.js (and Ember Data) allows you to specify model properties such as id , label and description . But I allow my users to add their own properties to any model, which is obviously impossible for me to know. Is there a way to dynamically add model properties at runtime? (And more importantly, will Ember.js recognize changes to these properties and save them?)

+6
source share
1 answer

Ember defines object properties through Ember.defineProperty . Signature Ember.defineProperty(object, propertyName, function) . For example, to define the lorem property supported by _lorem , you can use

 Ember.defineProperty(this, 'lorem', Ember.computed(function (key, value) { if (value) { this.set('_lorem', value); return value; } else { return this.get('_lorem'); } } )); 

You can call this method based on a user input handler when propertyName is dynamic. Such a thing is best suited for expanding the Meta object system. Caution is recommended, especially when entering a user!

+13
source

Source: https://habr.com/ru/post/949220/


All Articles