Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

按照文档写的例子没有生效 #18

Open
PreciousDust opened this issue Jun 12, 2019 · 3 comments
Open

按照文档写的例子没有生效 #18

PreciousDust opened this issue Jun 12, 2019 · 3 comments

Comments

@PreciousDust
Copy link

PreciousDust commented Jun 12, 2019

版本

2.1.0

问题

import { update, set } from 'san-update'
let source = {
       value: 1
}
update(source, {value: {$set: 2}})
set(source, 'value', 2)
console.log(source)
结果还是 {
   value:1
}
@otakustay
Copy link
Collaborator

let target = update(source, {value: {$set: 2}});
console.log(target);

正是因为这是一个immutable的库,所以source是不会变的

@PreciousDust
Copy link
Author

PreciousDust commented Jun 13, 2019

    initData() {
        return {
            list: [
                {
                    title: 'test1'
                },
                {
                    title: 'test2'
                }

            ]
        };
    }
       // 想改变对象深层的数据,下面仅仅是一个例子,实际上还有很多复杂的修改,不能直接用 san 提供的 this.data.set 修改,那么除了深拷贝解决没有更新的问题,san-update 也没办法解决这个问题吗?
        let firstNews = this.data.get('list');
        firstNews[0].title = 1111;

        // 在这里直接set数据发现并没有触发视图的更新
        this.data.set('list', firstNews);
        // 在这里深拷贝视图更新了
        this.data.set('list',  clone(firstNews));

@otakustay
Copy link
Collaborator

let firstNews = this.data.get('list');
let newFirstNews = set(firstNews, [0, 'title'], 1111);
this.data.set('list', newFirstNews);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants