# Useful Ember.js Functions

While developing stuff with Ember.js I stumbled upon some very nice and handy functions which are built in. This post lists some of them and describes in a short what they are used for.

Hey! This article references a pre-release version of Ember.js. Now that Ember.js has reached a 1.0 API the code samples below are no longer correct and the expressed opinions may no longer be accurate.

## Ember

The following functions are defined in packages/ember-runtime/lib/core.js and packages/ember-metal/lib/utils.js respectively.

### none

Returns true if argument is null or undefined.

### empty

This utility function constrains the rules on Ember.none by returning false for empty string and empty arrays.

### isArray

Use this to check if a value is an array. This method also returns true for array-like objects, like the ones implementing Ember.Array.

### makeArray

If you need a given object to be an array you can use this function. If the passed object is already an array it simply returns it, otherwise it creates an array of length 1, where the passed object is the only item.

### typeOf

Get the type of the passed argument.

### compare

This function compares two objects of possibly different types. It returns -1 if the first argument is smaller, 0 if both are equal and 1 if the first is greater than the second. The comparison order of different types is defined in Ember.ORDER_DEFINITIONS.

### isEqual

Checks if the passed two arguments are logically equal. If the first passed object has a isEqual, this function is used. The contents of two arrays are not compared. As stated in the corresponding test, this would be too expensive.

### copy

Cloning an object can be done via Ember.copy. If the object has the Ember.Copyable mixin its copy function is used. You can create a deep copy of the object by passing true as second argument.

### inspect

This function is kinda useful during debugging. It returns a string description of a given object.

### K

Ember.K simply is a function returning this. This might sound not very useful but I use it often during developing a view using the action helper. Because an error is thrown when a specified target cannot handle the triggered action I simply create a property and assign it to Ember.K and continue writing the view. You can even use the shorter alias Em.K.

### LOG_BINDINGS

This isn’t a function but a boolean flag. If set to true Ember will log all the activity that is happening on the bindings and you’ll get a insight of whats happening below the surface.

Tom Dale talked about this at the Ember.js Meetup, held on 21. February 2012. I encourage you to watch this video if you haven’t already seen it. And watch it again if you already have. It’s about debugging Ember.js apps and it’s shipped with very nice humor, bro.

## String

The functions described below are directly available on String’s unless Ember.EXTEND_PROTOTYPES is set to false as described here.

### w()

This method extracts each word inside a string separated by spaces. This saves some characters when assigning classNames to an Ember.View

### fmt()

Formatting of a string can be done via fmt() function. It simply replaces all occurrences of %@ inside a string with the passed arguments. The order of replacement can also be specified.

### loc()

This method formats the passed string but first checks if its a key in the Ember.STRINGS hash. By doing so it is possible to have a central point for defining the strings used in an application. This also allows internationalization. As stated in the source code its recommended - though not required - to prefix such a string with an underscore so it can be easily distinguished from ‘normal’ strings.

### More useful String functions

There are some more functionalities for converting between different String notations: camelize(), decamelize(), dasherize(), underscore()