This series of articles is all from ( http://ibeginner.sinaapp.com/ ), please visit the original website.
in the in Ember, enumerations are objects that include several child objects, and many of the Ember.enumerable APIs are provided to get the contained child objects. Our common enumerations are implemented based on native JavaScript arrays, andEmber extends many of these interfaces.
Ember provides a standardized interface to handle enumerations, and allows developers to completely change the underlying data store without having to modify other parts of the application to access it.
Ember 's Enumerable API complies with the ECMASCRIPT specification as much as possible . To reduce incompatibility with other libraries Ember.js allows you to implement arrays using a local browser.
Here are some of the reusable API Please note the difference between the left and right columns.
< P style= "Text-align:center" > standard method |
observable method |
description |
pop |
popobject |
This function removes the last item from the array, and returns the deleted item. |
push |
pushobject |
new element |
reverse |
reverseobject |
inverted array element |
shift |
shiftobject |
Remove the first element of the array from it and return the value of the first element |
Unshift |
Unshiftobject |
You can add one or more elements to the beginning of the array and return the new length. |
For detailed documentation, see:http://emberjs.com/api/classes/Ember.Enumerable.html
the method on the right side of the list is Ember rewrite the standard JavaScript method, their biggest difference is that an array of operations using the normal method (the left part) will not be automatically updated in your application (it will not trigger the observer), and the Ember The overridden method can trigger the observer, as long as your data changes Ember can be observed.
1, Array iterator
Iterate through the array elements using the ForEach method.
var arr = [' Chen ', ' UBUNTUVM ', ' [email protected] ', ' i2cao.xyz ', ' ubuntuvim.xyz '];arr.foreach (function (item, index) {con Sole.log (index+1 + "," +item);});
2, gets the array end-to-end element
Get the elements of the kinsoku, directly call the Ember encapsulated Firstobject and Lastobject method can be Console.log (' The FirstItem is ' + arr.get (' firstobject ')); Output> Chenconsole.log (' The LastItem is ' + arr.get (' lastobject ')); Output> ubuntuvim.xyz
3,MapMethod
The map method, which converts an array, and can add its own logic/map method to the callback function creates a new array and returns the elements of the converted array var arrmap = Arr.map (function (item) {return ' map: ' + item; Add your own required logical processing}); Arrmap.foreach (function (item, index) {console.log (item);}); Console.log ('-----------------------------------------------');
4,MapbyMethod
Mapby method: Returns the collection of object properties,//When your array element is an object, you can get the corresponding value according to the object's property name var obj1 = Ember.Object.create ({username: ' 123 ', age:25}); var obj2 = Ember.Object.create ({username: ' name ', age:35}); var obj3 = Ember.Object.create ({username: ' user ', Age:4 0}); var obj4 = Ember.Object.create ({age:40}); var arrobj = [Obj1, Obj2, Obj3, Obj4]; Object array var tmp = Arrobj.mapby (' username '); Tmp.foreach (function (item, index) {console.log (index+1+ "," +item);}); Console.log ('-----------------------------------------------');
5,FilterMethod
Filter Filter method, filters the normal array element//filter method can filter out the mismatched data with the conditions you specify, such as the following code: Filter elements greater than 4 var nums = [1, 2, 3, 4, 5];//parameter self value array itself var n umstmp = Nums.filter (function (item, index, self) {if (Item < 4) return true;}); Numstmp.foreach (function (item, index) {console.log (' item = ' + Item); 1, 2, 3}); Console.log ('-----------------------------------------------');
6,FilterbyMethod
If you want to filter an array based on an attribute of an object you need to use the Filterby method, such as the following code to filter the object properties according to Isdone var o1 = Ember.Object.create ({ name: ' U1 ', isdone: true}); var o2 = ember.object.create ({ name: ' U2 ', isdone: false}); var o3 = ember.object.create ({ name: ' U3 ', isdone: true}); var O4 = ember.object.create ({ name: ' U4 ', isdone: true}); var todos = [o1, o2, o3, o4];var isdonearr = todos.filterby (' IsDone ' , true); //will filter out the O2 Isdonearr.foreach (function (item, index) { console.log (' name = ' + item.get (' name ') + ', isdone = ' + item.get ( ' IsDone '); // console.log (item);}); console.log ('-----------------------------------------------');
7,Find,FindByMethod
The Find and FindBy methods, both of which return only the first matched element, are similar to filter and Filterby, one for returning the normal element, one for the object property to judge var firstnum = Nums.find (function ( Item, index, SEFT) {return item < 4;}); Console.log (' Firstnum = ' + Firstnum); var firstobjattr = todos.findby (' IsDone ', true); Console.log (' name = ' + firstobjattr.get (' name ') + ', IsDone = ' + Firstob Jattr.get (' IsDone ')); Console.log ('-----------------------------------------------');
8,Every,SomeMethod
The every, some method// every is used to determine whether all elements of an array meet the criteria, returns True if all elements conform to the specified criteria, or returns False// some is used to judge all elements of an array returns true if one element matches the condition, otherwise returns Falseperson = ember.object.extend ({ name: Null, ishappy: true}); Var people = [ person.create ({ name: ' Chen ', ishappy: true }), person.create ({ name: ' Ubuntuvim ', ishappy: false }), person.create ({ name: ' i2cao.xyz ', isHappy: true }), person.create ({ name: ' 123 ', ishappy: false }), Person.create ({ name: ' ibeginner.sinaapp.com ', ishappy: false })];var every = people.every (function (person, index, self) { if (Person.get (' Ishappy ') return true;}); Console.log (' every = ' + every); var some = people.somE (function (person, index, self) { if (Person.get (' Ishappy ')) return true;}); Console.log (' some = ' + some);
9,Isevery,IsAnyMethod
Similar methods to every and some are isevery, IsAny console.log (' isevery = ' + people.isevery (' Ishappy ', true)); All is true, and the result is trueconsole.log (' IsAny = ' + people.isany (' Ishappy ', true)); As long as there is a true, the return result is true
The above method is basically similar to the method provided by other JS frameworks. Learning is not very difficult ...
Ember.js Getting Started Guide--enumeration (Enumerables)