[Immutable + AngularJS] Use immutable. List () for Angular array

Source: Internet
Author: User

Const STORES = immutable.list([{name:' Store42 ', Position: {Latitude:61.45, Longitude:23.11,}, Address:' Whatever '}, {name:' Store2 ', Position: {Latitude:61.48, Longitude:23.87}, Address:' Whatever '}, {name:' Store3 ', Position: {Latitude:61.41, Longitude:23.76}, Address:' Whatever '}, {name:' Store4 ', Position: {Latitude:61.42, Longitude:23.40}, Address:' Whatever '}]); class Storeservice {constructor ($q) { This. $q =$q;  This. Mockstores =stores; } getstores (position) {return  This. $q ((Resolve) = {            returnResolve This. Mockstores.        ToArray () );    } ); }}exportdefault(ngmodule) ={Ngmodule.service (' Storeservice ', Storeservice);}

Try to only keep ' serivce ' to deal with immutable data, controller should has no knowledge about whether the data in Muta BLE type or immutable type.

When you return the data to the controller, we Sue '. ToArray () ' method to convert the immutable data structure to normal Ja Vascript Array method.

------------------------------

The reason here we use both ' const ' and immutable are because:

Const _person = {            "Zhentian"        };class Storeservice {    Constructor (  ) {        this . person = _person;         this. Person.name = "John";         // - John Default (Ngmodule) = {    ' storeservice ', Storeservice);}      

In the contructor we define: ' _person ' assign to ' This.person ', even _person are const type, but if we try to modiy the Data Thougth This.person object, we found actually we are able to do.

So the const isn't safe when deal with object!

So we still need immutable to help us to keep data immutable.

[Immutable + AngularJS] Use immutable. List () for Angular array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.