使用Ember.Object
的get()
檢索值和set()
方法來更新值。
hasPermission.get('someProperty');
hasPermission.set('someProperty', 'New value of someProperty');
其中someProperty
可以在模型中定義:
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
export default Model.extend({
someProperty: attr('string');
});
使用這些方法也將確保計算的屬性重新計算(默認情況下,如果根據值同時已改變)。我強烈建議閱讀Ember指南的這一部分:The Object model。
順便說一句,就個人而言,我從JSHint切換到ESLint和施加eslint-plugin-ember,我使用從灰燼namespaceget()
和set()
方法。 在這種情況下,您不會寫hasPermission.get('data')
,而會寫Ember.get(hasPermission, 'data');
。把它更進一步,當涉及到代碼的可讀性,你可以做某事像這樣:
// your-app/services/your-service.js
// ... imports
const {
get,
inject,
Service,
} = Ember;
export default Service.extend({
store: inject.service(),
someFunction(){
const hasPermission = this.get('store').peekRecord('system/permission','PlatformRead');
console.log(get(hasPermission, 'someProperty'));
},
});
以下#1 thread觸摸略有this.get()
和Ember.get()
之間的差異。