[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.

posted @   Zhentiw  阅读(421)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示