如何在Dojo中更改Object Store Model的默认值?

时间:2015-02-04 10:45:29

标签: javascript dojo

我正在使用Dojo ObjectStoreModel作为dijit / Tree小部件。 我需要配置ObjectStoreModel以使用名为parent_id的属性而不是默认的parent来匹配我的数据结构。

知道如何设置此配置吗?

   var data = [
      {
        id: 'world',
        name: 'The earth',
        type: 'planet',
        population: '6 billion'
      },
      {
        id: 'EG',
        name: 'Egypt',
        type: 'country',
        parent_id: 'AF' // does not work if property is called parent_id, only parent by default
      },
      {
        id: 'Nairobi',
        name: 'Nairobi',
        type: 'city',
        parent_id: 'KE'
      },
      {
        id: 'Mombasa',
        name: 'Mombasa',
        type: 'city',
        parent_id: 'KE'
      },
      {
        id: 'SD',
        name: 'Sudan',
        type: 'country',
        parent_id: 'AF'
      },

    ];


            var myStore = new Memory({
                data: data,
                getChildren: function (object) {
                    return this.query({ parent: object.id });
                }
            });

            // Create the model
            var myModel = new ObjectStoreModel({
                store: myStore,
                query: { id: '0' }
            });


            // Create the Tree.
            var tree = new Tree({
                model: myModel
            });
            tree.placeAt(this.domNode);
            tree.startup();

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法

http://dojotoolkit.org/reference-guide/1.10/dijit/tree/ObjectStoreModel.html

陈述:The dojo.store must implement it’s own getChildren() method.

因此,使用此编辑脚本可以正常工作。

var myStore = new Memory({
                data: data,
                getChildren: function (object) {
                    return this.query({ parent_id: object.id });
                }
            });