mercredi 18 février 2015

Ember - Custom Computed Property to check if all dependent fields exists

I am creating a form and I am trying to find a simple, elegant way of handling to see if all inputs exist.



Form = Ember.Object.extend({
// section 1
name: null,
age: null,
isABoolean: null,

// section 2
job: null,
numberOfSiblings: null,

isComplete: Ember.computed.and('_isSection1Complete', '_isSection2Complete'),

_isSection1Complete: function() {
var isPresent = Ember.isPresent;
return isPresent(this.get('name')) && isPresent(this.get('age')) && isPresent(this.get('isABoolean'));
}.property('name', 'age', 'isABoolean'),

_isSection2Complete: function() {
var isPresent = Ember.isPresent;
return isPresent(this.get('job')) && isPresent(this.get('numberOfSiblings'));
}.property('job', 'numberOfSiblings')
});


However, this doesn't seem to scale. My actual application will have many sections (over 20 sections).


I am looking into trying to create a re-usable computed property that fits my needs. Take for example the code of what I am going for:



Form = Ember.Object.extend({
// properties...

isComplete: Ember.computed.and('_isSection1Complete', '_isSection2Complete'),

_isSection1Complete: Ember.computed.allPresent('name', 'age', 'isABoolean'),

_isSection2Complete: Ember.computed.allPresent('job', 'numberOfSiblings')
});


I feel that this is a common case, but I'm failing to find the correct computed properties on how to execute this, so I would like to make my own.


Two questions:



  1. Where's the best place to define the custom computed property? Can I just attach a function to Ember.computed?

  2. Is there an easier way to solve this? I feel like I'm overlooking something simple.


Aucun commentaire:

Enregistrer un commentaire