Wednesday, October 29, 2014
Useful Functions in the Ember Namespace
Have you been frustrated at all with Ember because of a lack of common functionality? Well, if you are you are probably like how I was and forgot to read the manual. There are a lot of really excellent functions baked right into the Ember. Here are some really useful ones that may come in handy and save you a few keystrokes. Many of the examples below are taken from the Ember API docs and a few are taken from the Discourse source code.
Define an assertion that will throw an exception if the condition is not met. Ember build tools will remove any calls to Ember.assert() when doing a production build.
Ember.assert("should pass", 1 === 1) //=> undefinedEmber.assert("should fail", 1 === 2) //=> Assertion failed: should fail// You can force a failed assertion by not passing the boolean argumentEmber.assert("force fail") //=> Assertion failed: force failhttp://emberjs.com/api/#method_assert
You can easily create computed properties with the computed property helper functions:
http://emberjs.com/api/#method_computed_and
Performs the logical and on the dependentKeys provided and returns a boolean if all resolve to true.
showMessageInput: Ember.computed.and('is_custom_flag', 'selected')http://emberjs.com/api/#method_computed_any
Returns the first truthy value of a given list of properties.
isAuthorized: Ember.computed.any('user_id', 'auth_token')http://emberjs.com/api/#method_computed_bool
Convert to boolean the original value for property.
isSorted: Ember.computed.bool('sortProperties')Negate the original value for property.
sendTestEmailDisabled: Ember.computed.empty('testEmailAddress')http://emberjs.com/api/#method_computed_equal
Computed property which returns true if the original value for property is equal to the given value.
markdown: Ember.computed.equal('format', 'markdown'),plainText: Ember.computed.equal('format', 'plain'),html: Ember.computed.equal('format', 'html'),css: Ember.computed.equal('format', 'css')http://emberjs.com/api/#method_computed_gt
Computed property which returns true if the original value for property is greater than the given value.
showContinueButton: Ember.computed.gt('minimumSongsSelected', 'songCount')http://emberjs.com/api/#method_computed_gte
Computed property which returns true if the original value for property is greater than or equal to the given value.
isOldEnough: Ember.computed.gte('minimumAge', 'age')http://emberjs.com/api/#method_computed_lt
Computed property which returns true if the original value for property is less than the given value.
needMoreSongs: Ember.computed.lt('minimumSongsSelected', 'songCount')http://emberjs.com/api/#method_computed_lte
Computed property which returns true if the original value for property is less than or equal to the given value.
showContinueButton: Ember.computed.lte('maximumFavoriteGenres', 'chosenGenreCount')http://emberjs.com/api/#method_computed_map
Computed property which maps values of all passed properties in to an array.
JS Bin
http://emberjs.com/api/#method_computed_match
Computed property which match the original value for property against a given RegExp.
JS Bin
http://emberjs.com/api/#method_computed_none
Computed property which returns true if original value for property is null or undefined.
showInstructions: Ember.computed.none('email')http://emberjs.com/api/#method_computed_not
submitDisabled: Ember.computed.not('submitEnabled')http://emberjs.com/api/#method_computed_notEmpty
Computed property which returns true if original value for property is not empty.
visible: Ember.computed.notEmpty('controller.buffer')http://emberjs.com/api/#method_computed_oneWay
Computed property which creates an one way computed property to the original value for property. Where computed.alias aliases get and set, and allows for bidirectional data flow, computed.oneWay only provides an aliased get. The set will not mutate the upstream property, rather causes the current property to become the value set. This causes the downstream property to permentantly diverge from the upstream property.
User = Ember.Object.extend({ firstName: null, lastName: null, nickName: Ember.computed.oneWay('firstName')});user = User.create({ firstName: 'Teddy', lastName: 'Zeenny' });user.get('nickName'); //=> 'Teddy' user.set('nickName', 'TeddyBear'); //=> 'TeddyBear' user.get('firstName'); //=> 'Teddy'http://emberjs.com/api/#method_computed_or
Computed property which peforms a logical or on the values of all the original values for properties.
showManagerTools: Ember.computed.and('is_admin', 'is_manager')Display a debug notice. Ember build tools will remove any calls to Ember.debug() when doing a production build.
Ember.debug("I'm a debug notice!");http://emberjs.com/api/#method_debug
Tears down the meta on an object so that it can be garbage collected. Multiple calls will have no effect.
http://emberjs.com/api/#method_destroy
Returns true if the passed object is an array or Array-like.
Ember.isArray(); // falseEmber.isArray([]); // trueEmber.isArray( Ember.ArrayProxy.create({ content: [] }) ); // truehttp://emberjs.com/api/#method_isArray
Verifies that a value is null or an empty string, empty array, or empty function.
Ember.isEmpty(); // trueEmber.isEmpty(null); // trueEmber.isEmpty(undefined); // trueEmber.isEmpty(''); // trueEmber.isEmpty([]); // trueEmber.isEmpty('Adam Hawkins'); // falseEmber.isEmpty([0,1,2]); // falsehttp://emberjs.com/api/#method_isEmpty
Compares two objects, returning true if they are logically equal. This is a deeper comparison than a simple triple equal. For sets it will compare the internal objects. For any other object that implements isEqual() it will respect that method.
Ember.isEqual('hello', 'hello'); // trueEmber.isEqual(1, 2); // falseEmber.isEqual([4,2], [4,2]); // falsehttp://emberjs.com/api/#method_isEqual
Returns true if the passed value is null or undefined. This avoids errors from JSLint complaining about use of ==, which can be technically confusing.
Ember.isNone(); // trueEmber.isNone(null); // trueEmber.isNone(undefined); // trueEmber.isNone(''); // falseEmber.isNone([]); // falseEmber.isNone(function(){}); // falsehttp://emberjs.com/api/#method_isNone
Forces the passed object to be part of an array. If the object is already an array or array-like, returns the object. Otherwise adds the object to an array. If obj is null or undefined, returns an empty array.
Ember.makeArray(); // []Ember.makeArray(null); // []Ember.makeArray(undefined); // []Ember.makeArray('lindsay'); // ['lindsay']Ember.makeArray([1,2,42]); // [1,2,42]var controller = Ember.ArrayProxy.create({ content: [] });Ember.makeArray(controller) === controller; // truehttp://emberjs.com/api/#method_makeArray
Checks to see if the methodName exists on the obj, and if it does, invokes it with the arguments passed.
http://emberjs.com/api/#method_tryInvoke
Error-tolerant form of Ember.set. Will not blow up if any part of the chain is undefined, null, or destroyed.
This is primarily used when syncing bindings, which may try to update after an object has been destroyed.
Ember.trySet('model', 'firstName', 'Eric')http://emberjs.com/api/#method_trySet
No comments: