[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.
[Immutable + AngularJS] Use Immutable .List() for Angular array的更多相关文章
- [Immutable.js] Transforming Immutable Data with Reduce
Immutable.js iterables offer the reduce() method, a powerful and often misunderstood functional oper ...
- Immutable Collections(3)Immutable List实现原理(中)变化中的不变
Immutable Collections(3)Immutable List实现原理(中)变化中的不变 文/玄魂 前言 在上一篇文章(Immutable Collections(2)Immutabl ...
- [AngularJS] Isolate State Mutations in Angular Components
Managing state is one of the hardest things to do in any application. Angular 2 tackles this problem ...
- [Immutable.js] Converting Immutable.js Structures to Javascript and other Immutable Types
Immutable.js provides several conversion methods to migrate one structure to another. Each Immutable ...
- AngularJS开发指南3:Angular主要组成部分以及如何协同工作
AngularJS的主要组成部分是: 启动(startup) - 展示“hello world!” 执行期(runtime) - AngularJS 执行期概览 作用域(scope) - 视图和控制器 ...
- AngularJS进阶(三十四)Angular数据更新不及时问题探讨
Angular数据更新不及时问题探讨 前言 在修复控制角标正确变化过程中,发觉前端代码组织层次出现了严重问题.传递和共享数据时自己使用的是rootScope,为此造成了全局变量空间的污染.根据< ...
- AngularJS入门讲解1:angular基本概念
AngularJS应用程序主要有三个组成部分: 模板(Templates) 模板是您用HTML和CSS编写的文件,展现应用的视图. 您可给HTML添加新的元素.属性标记,作为AngularJS编译器的 ...
- AngularJS学习(二)——Angular应用的解析
本节描述AngularJS应用程序的三个组成部分,并解释它们如何映射到模型-视图-控制器设计模式 模板(Template) 模板是您用HTML和CSS编写的文件,展现应用的视图.您可给HTML添加新的 ...
- [AngularJS] Adding custom methods to angular.module
There are situations where you might want to add additional methods toangular.module. This is easy t ...
随机推荐
- DataBindings 与 INotifyPropertyChanged 实现自动刷新 WinForm 界面
--首发于博客园, 转载请保留此链接 博客原文地址 业务逻辑与界面的分离对于维护与迁移是非常重要的,在界面上给某属性赋值,后台要检测到其已经发生变化 问题: 输入某物品 单价 Price, 数量Am ...
- open(),close() 打开/关闭文件
Open open()是一个系统调用函数,用来打开或创建一个文件,通过不同的oflag选项实现不同功能. 使用时open()函数需要包含的头文件:<sys/types.h>,<sys ...
- oracle监听服务开启
输入命令netca即可开启oracle的监听服务 弹出对话框 选择监听服务配置,单击下一步 选择增加监听,单击下一步 监听的名字,默认即可,下一步 监听链接的协议,默认TCP协议即可,下一步 监听默认 ...
- MySql查看表信息
SELECT TABLE_NAME, TABLE_COMMENT -- 指定信息列 FROM `information_schema`.`tables` A WHERE A.`TABLE_SCHEMA ...
- iCIBA简单案例
效果图: 代码: <!DOCTYPE html><html> <head> <meta charset="utf-8" /> < ...
- js html5 仿微信摇一摇
看微信摇一摇之后忽然想知道他是怎么写的.于是就在网上查了一些资料,有些是借鉴别人的.大家共同学习啊 先放代码 <body onload="init()"> <p& ...
- silverlight+wcf 获得web参数
可以由wcf直接得到参数 ,具体代码如下: using System;using System.Linq;using System.Runtime.Serialization;using System ...
- jQuery封装的表单验证,模仿网易或者腾讯登录的风格
模仿网易邮箱做了一个登录表单验证,不太好,请指教 上代码 <form action="" name="" id="form1"> ...
- windows下使用php重命名目录下的文件
rename函数一直报错,最后发现是windows下文件名的编码问题,如果项目文件是utf-8的话,一定要经过一步转码 $dir = $path . '/../resource/logo'; $han ...
- Django QuerySets 里的**kwargs: 动态创建ORM查询
Django的数据库API查询经常包含关键字参数.例如: bob_stories = Story.objects.filter(title_contains='bob', subtitle_conta ...