mardi 6 octobre 2015

Required properties (constructor args) in Ember.Object instances

In Ember, let's say I have an object called FoodStuff that has a few properties:

export default Ember.Object.extend({
    name: null,     // REQUIRED: 'Slice of Apple Pie'
    calories: null, // OPTIONAL: int: eg. 250
    category: null, // REQUIRED: 'Pastry'
    rating: null    // OPTIONAL: int: 1-5
});

How can I write a 'constructor' in Ember, requiring that the 'name' and 'category' properties be provided at instantiation-time?

Angular seems to approach this with fairly straightforward syntax:

.factory('User', function (Organisation) {

  /**
   * Constructor, with class name
   */
  function User(firstName, lastName, role, organisation) {
    // Public properties, assigned to the instance ('this')
    this.firstName = firstName;
    ...

Angular model objects with JavaScript classes

Does Ember have something similar? Currently all my classes are as seen at the top, with a bunch of initially null properties that might or might not be properly set by the caller. At build time (I'm using ember-cli) I would like for changes in constructor requirements to be caught downstream by the ember build phase.




Aucun commentaire:

Enregistrer un commentaire