Created
July 23, 2015 07:48
-
-
Save GavinJoyce/6f60a0a6b0b3a6aba3f6 to your computer and use it in GitHub Desktop.
DublinJS / EmberJS Dublin Computed Property Macros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Controller | |
import Em from 'ember'; | |
function concatProperties(key1, key2) { | |
return Em.computed(key1, key2, function() { | |
return this.get(key1) + ' ' + this.get(key2); | |
}); | |
} | |
var fmt = function() { //a computed function macro similar to https://github.com/cibernox/ember-cpm/blob/master/addon/macros/fmt.js | |
var params = Array.prototype.slice.call(arguments); | |
var template = params.shift(); | |
var keys = params.slice(0); | |
params.push(function() { | |
var values = []; | |
for (var i = 0; i < keys.length; ++i) { | |
values.push(Em.get(this, keys[i])); | |
} | |
return Em.String.fmt(template, values); | |
}); | |
return Em.computed.apply(this, params); | |
}; | |
export default Em.Controller.extend({ | |
firstName: 'Paddy', | |
lastName: 'Smith', | |
fullName: function() { | |
return this.get('firstName') + ' ' + this.get('lastName'); | |
}.property('firstName', 'lastName'), | |
fullName2: Em.computed('firstName', 'lastName', function() { | |
return this.get('firstName') + ' ' + this.get('lastName'); | |
}), | |
fullName3: concatProperties('firstName', 'lastName'), | |
fullName4: fmt('%@ %@', 'firstName', 'lastName'), | |
fullName5: fmt('Hello %@, your surname is [%@] which has a length of %@', 'firstName', 'lastName', 'lastName.length'), | |
}); | |
//template | |
<h3>hello properties</h3> | |
firstName: {{input value=firstName}}<br /> | |
lastName: {{input value=lastName}}<br /> | |
<hr /> | |
Hi {{firstName}} {{lastName}} | |
<hr /> | |
fullName: {{fullName}}<br /> | |
fullName2: {{fullName2}}<br /> | |
fullName3: {{fullName3}}<br /> | |
fullName4: {{fullName4}}<br /> | |
fullName5: {{fullName5}}<br /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment