coderberry

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.

Ember.assert(desc, test)

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.

1
2
3
4
5
Ember.assert("should pass", 1 === 1) //=> undefined
Ember.assert("should fail", 1 === 2) //=> Assertion failed: should fail

// You can force a failed assertion by not passing the boolean argument
Ember.assert("force fail")           //=> Assertion failed: force fail

http://emberjs.com/api/#method_assert

Ember.computed.SOMETHING

You can easily create computed properties with the computed property helper functions:

http://emberjs.com/api/#method_computed_and

and(dependentKey,)

Performs the logical and on the dependentKeys provided and returns a boolean if all resolve to true.

1
showMessageInput: Ember.computed.and('is_custom_flag', 'selected')

http://emberjs.com/api/#method_computed_any

any(dependentKey,)

Returns the first truthy value of a given list of properties.

1
isAuthorized: Ember.computed.any('user_id', 'auth_token')

http://emberjs.com/api/#method_computed_bool

bool(dependentKey)

Convert to boolean the original value for property.

1
isSorted: Ember.computed.bool('sortProperties')

empty(dependentKey)

Negate the original value for property.

1
sendTestEmailDisabled: Ember.computed.empty('testEmailAddress')

http://emberjs.com/api/#method_computed_equal

equal(dependentKey, value)

Computed property which returns true if the original value for property is equal to the given value.

1
2
3
4
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

gt(dependentKey, value)

Computed property which returns true if the original value for property is greater than the given value.

1
showContinueButton: Ember.computed.gt('minimumSongsSelected', 'songCount')

http://emberjs.com/api/#method_computed_gte

gte(dependentKey, value)

Computed property which returns true if the original value for property is greater than or equal to the given value.

1
isOldEnough: Ember.computed.gte('minimumAge', 'age')

http://emberjs.com/api/#method_computed_lt

lt(dependentKey, value)

Computed property which returns true if the original value for property is less than the given value.

1
needMoreSongs: Ember.computed.lt('minimumSongsSelected', 'songCount')

http://emberjs.com/api/#method_computed_lte

lte(dependentKey, value)

Computed property which returns true if the original value for property is less than or equal to the given value.

1
showContinueButton: Ember.computed.lte('maximumFavoriteGenres', 'chosenGenreCount')

http://emberjs.com/api/#method_computed_map

map(dependentKey,)

Computed property which maps values of all passed properties in to an array.

JS Bin

http://emberjs.com/api/#method_computed_match

match(dependentKey, regExp)

Computed property which match the original value for property against a given RegExp.

JS Bin

http://emberjs.com/api/#method_computed_none

computed.none(dependentKey)

Computed property which returns true if original value for property is null or undefined.

1
showInstructions: Ember.computed.none('email')

http://emberjs.com/api/#method_computed_not

computed.not(dependentKey)

1
submitDisabled: Ember.computed.not('submitEnabled')

http://emberjs.com/api/#method_computed_notEmpty

computed.notEmpty(dependentKey)

Computed property which returns true if original value for property is not empty.

1
visible: Ember.computed.notEmpty('controller.buffer')

http://emberjs.com/api/#method_computed_oneWay

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.

1
2
3
4
5
6
7
8
9
10
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.or

Computed property which peforms a logical or on the values of all the original values for properties.

1
showManagerTools: Ember.computed.and('is_admin', 'is_manager')

Ember.debug(message)

Display a debug notice. Ember build tools will remove any calls to Ember.debug() when doing a production build.

1
Ember.debug("I'm a debug notice!");

http://emberjs.com/api/#method_debug

Ember.destroy(obj)

Tears down the meta on an object so that it can be garbage collected. Multiple calls will have no effect.

1
Ember.destroy(session)

http://emberjs.com/api/#method_destroy

Ember.isArray(obj)

Returns true if the passed object is an array or Array-like.

1
2
3
Ember.isArray();                                            // false
Ember.isArray([]);                                          // true
Ember.isArray( Ember.ArrayProxy.create({ content: [] }) );  // true

http://emberjs.com/api/#method_isArray

Ember.isEmpty(obj)

Verifies that a value is null or an empty string, empty array, or empty function.

1
2
3
4
5
6
7
Ember.isEmpty();                // true
Ember.isEmpty(null);            // true
Ember.isEmpty(undefined);       // true
Ember.isEmpty('');              // true
Ember.isEmpty([]);              // true
Ember.isEmpty('Adam Hawkins');  // false
Ember.isEmpty([0,1,2]);         // false

http://emberjs.com/api/#method_isEmpty

Ember.isEqual(a, b)

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.

1
2
3
Ember.isEqual('hello', 'hello');  // true
Ember.isEqual(1, 2);              // false
Ember.isEqual([4,2], [4,2]);      // false

http://emberjs.com/api/#method_isEqual

Ember.isNone(obj)

Returns true if the passed value is null or undefined. This avoids errors from JSLint complaining about use of ==, which can be technically confusing.

1
2
3
4
5
6
Ember.isNone();              // true
Ember.isNone(null);          // true
Ember.isNone(undefined);     // true
Ember.isNone('');            // false
Ember.isNone([]);            // false
Ember.isNone(function(){});  // false

http://emberjs.com/api/#method_isNone

Ember.makeArray(obj)

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.

1
2
3
4
5
6
7
8
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;  // true

http://emberjs.com/api/#method_makeArray

Ember.tryInvoke(obj, methodName, args)

Checks to see if the methodName exists on the obj, and if it does, invokes it with the arguments passed.

1
Ember.tryInvoke('model', 'deliverWelcomeEmail', '[email protected]')

http://emberjs.com/api/#method_tryInvoke

Ember.trySet(obj, methodName, args)

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.

1
Ember.trySet('model', 'firstName', 'Eric')

http://emberjs.com/api/#method_trySet

Comments