zoukankan      html  css  js  c++  java
  • [Immutable + AngularJS] Use Immutable .List() for Angular array

            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 )=> {
                return resolve( this.mockStores.toArray() );
            } );
        }
    }
    
    export default ( ngModule ) => {
        ngModule.service( 'StoreService', StoreService );
    }

    Try to only keep 'serivce' to deal with Immutable data, controller should have no knowledge about whether the data in mutable type or immutable type.

    So when return the data to the controller, we sue '.toArray()' method to convert the Immutable data structure to normal Javascript array method.

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

    The reason here we use both 'const' and Immutable is because:

     const _person = {
                name: "Zhentian"
            };
    
    class StoreService {
        constructor(  ) {
            this.person = _person;
    
            this.person.name = "John";
            console.log(_person.name); // --> John
    
        }
    
    }
    
    export default ( ngModule ) => {
        ngModule.service( 'StoreService', StoreService );
    }

    In the contructor we define:  '_person' assign to 'this.person', even _person is const type, but when we try to modiy the data thougth this.person object, we found actually we are able to do that.

    So the const is not safe when deal with object!

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

  • 相关阅读:
    菜单按钮及导航
    实现点击箭头切换图片页和相册滚动
    网页设计的基本原则
    网格系统
    表单系列2
    类与对象学习总结
    汉诺塔的最简的步骤思路
    3.31作业解答
    初学java 用if语句做几个小程序
    做三个java初期学习的练习Var1~3为头目标
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5202099.html
Copyright © 2011-2022 走看看