Non-persistent attributes in EmberJS

后端 未结 4 1230
时光取名叫无心
时光取名叫无心 2021-02-13 19:48

Does anyone know of a way to specify for an Ember model an attribute which is not persisted?

Basically, we\'re loading some metadata related to each model

4条回答
  •  悲&欢浪女
    2021-02-13 20:27

    When this PR get's merged it will be possible to flag properties as readOnly. But till then there are some workarounds to this, e.g. overriding your addAttributes method in the Adapter and deal with your special properties, here an example how this could look like:

    Define your Model by adding the new option readOnly:

    App.MyModel = DS.Model.extend({
      myMetaProperty: DS.attr('metaProperty', {readOnly: true})
    });
    

    and then on the Adapter:

    App.Serializer = DS.RESTSerializer.extend({
       addAttributes: function(data, record) {
         record.eachAttribute(function(name, attribute) {
           if (!attribute.options.readOnly) {
             this._addAttribute(data, record, name, attribute.type);
           }
         }, this);
       }
     });
    

    what this does is to loop over the attributes of your model and when it find's an attribute with the readOnly flag set it skips the property.

    I hope this mechanism works for your use case.

提交回复
热议问题