如何观察dstore根对象的变化?

时间:2016-08-23 19:29:21

标签: dojo

我的数据存储由一系列项目组成,我希望在数组中添加或删除项目时收到通知。我怎样才能做到这一点?我试图在整个数据上提取一个属性对象,但它没有成功。

<script>

    require(
        [
        'dojo/_base/declare',
        'dstore/Memory',
        'dmodel/extensions/jsonSchema',
        'dmodel/validators/StringValidator',
        'dmodel/store/Validating',
        "dmodel/Model",
        "dojox/json/schema",
        "dojo/text!app/model/testing/baseSchema.json",
        ], 
    function (declare, Memory, jsonSchema, StringValidator, Validating, Model, DJS, mySchema) {


        var validatingMemory2 = (declare([Memory, Validating]))({
            Model: jsonSchema(
                {
                    "$schema": "http://json-schema.org/draft-04/schema#",
                    "title": "Form Elements",
                    "type": "array",
                    "items": {
                        "title": "Form Element",
                        "type": "object",
                        "properties": {
                            "id": {
                                "description": "identifier",
                                "type": "string"
                            },

                            "positionX": {
                                "type": "number"
                            },

                            "positionY": {
                                "type": "number"
                            },
                            "moduleType": {
                                "description": "type",
                                "type": "string"
                            }

                        },
                        "required": ["id"],
                        "additionalProperties": false
                    }
                }

                ),

            idProperty: "id",

        });


        validatingMemory2.setData([{ "id": "one", "positionX": 100, "positionY": 200, "moduleType": "label" }]);
        console.log(validatingMemory2);

        //error: property is not a function
        var prpertyObject = validatingMemory2..property("items");

        //I want to be alerted here when the item is added
        validatingMemory2.addSync({ "id": "two", "positionX": 300, "positionY": 400, "moduleType": "label" }); 

        console.log(validatingMemory2);

    });
</script>

1 个答案:

答案 0 :(得分:1)

您应该在dstore中使用数据通知。

您可以使用可以通过on()方法注册的侦听器监视接收通知的数据更改,其中包含由不同事件类型设计的不同形式的通知:添加,更新和删除。

尝试在代码中使用它:

validatingMemory2 .on('delete, add, update', function(event){
    // execute when a change is made to your store
});

可在此处找到更多信息:

https://www.sitepen.com/blog/2014/11/17/introducing-dstore/

http://dstorejs.io/tutorials/realtime_stores.html