刷新模型更改的Dojo MVC组不适用于复杂对象

时间:2014-11-15 15:17:42

标签: javascript dom dojo requirejs

如下所示,我有一个虚拟内存存储区,它返回一个复杂的对象。我想将这个复杂的对象绑定到表单。因为它是一个复杂的对象,我通过两个MVC组绑定对象。刷新模型时会发生问题(通过" Fetch"按钮)。外部MVC组是刷新的,但内部MVC组不是。它只有在我手动重新设置内部MVC组的target属性时才有效:

dijit.byId('sub-group').set('target', model.prop7);

还有其他更好的方法吗?

<script type="text/javascript">
    // Create dummy store
    var store;
    require(['dojo/store/Memory'], function(Memory) {
        store = new Memory({
            data: [{
                id: 1,
                prop1: 'prop 1 value',
                prop2: 'WA',
                prop7: {
                    prop8: 'prop 4 value',
                    prop9: 'prop 5 value'
                }
            }]
        });
    });

    // Create model for MVC
    var model;
    require([
        'dojo/dom',
        "dojo/parser",
        "dojo/Stateful",
        "dojo/domReady!"
    ], function (dom, parser, Stateful) {
        model = new Stateful();
    });

</script>

<button data-dojo-type="dijit/form/Button" data-dojo-props="iconClass: 'dijitIconDatabase'">
    <script type="dojo/on" data-dojo-event="click">
        var data = store.get(1);

        model.set(data);

        //dijit.byId('sub-group').set('target', model.prop7);
    </script>

    Fetch
</button>


<form data-dojo-type="dijit/form/Form" id="form" data-dojo-type="dojox/mvc/Group" data-dojo-props="target: model">
    <script type="dojo/require">at: "dojox/mvc/at"</script>

    <div class="row">
        <label for="text">Text:</label>
        <input id="text" data-dojo-type="dijit/form/ValidationTextBox" required="true" data-dojo-props="value: at('rel:', 'prop1')"/>
    </div>

    <div class="row" id="sub-group" data-dojo-type="dojox/mvc/Group" data-dojo-props="target: model.prop7">
        <label for="text2">Text 2:</label>
        <input id="text2" data-dojo-type="dijit/form/ValidationTextBox" required="true" data-dojo-props="value: at('rel:', 'prop8')"/>
    </div>
</form>

1 个答案:

答案 0 :(得分:5)

我可以说几件事:

以下是类似用例的示例代码:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.1/dojo/resources/dojo.css" />
        <link id="themeStyles" rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.10.1/dijit/themes/claro/claro.css" />
        <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/dojo/1.10.1/dojo/dojo.js" data-dojo-config="parseOnLoad: 0, async: 1"></script>
        <script type="text/javascript">
            require([
                "dojo/parser",
                "dojo/store/Memory",
                "dojox/mvc/getStateful"
            ], function(parser, Memory, getStateful){
                model = getStateful({});
                store = new Memory({
                    data: [{
                        id: "mydata",
                        prop1stLevel: "Value of 1st level property",
                        group2ndLevel: {
                            prop2ndLevel: "Value of 2nd level property"
                        }
                    }]
                });
                parser.parse();
            });
        </script>
    </head>
    <body class="claro">
        <script type="dojo/require">at: "dojox/mvc/at"</script>
        <button data-dojo-type="dijit/form/Button" data-dojo-props="iconClass: 'dijitIconDatabase'">
            <script type="dojo/on" data-dojo-event="click">
                require(["dojox/mvc/getStateful"], function(getStateful){
                    model.set("group1stLevel", getStateful(store.get("mydata")));
                });
            </script>
            Fetch
        </button>
        <form id="form" data-dojo-type="dijit/form/Form" data-dojo-mixins="dojox/mvc/Group" data-dojo-props="target: at(model, 'group1stLevel')">
            <div class="row">
                <label for="1st-level">1st level:</label>
                <input id="1st-level" data-dojo-type="dijit/form/ValidationTextBox" required="true" data-dojo-props="value: at('rel:', 'prop1stLevel')"/>
            </div>
            <div class="row" id="sub-group" data-dojo-type="dojox/mvc/Group" data-dojo-props="target: at('rel:', 'group2ndLevel')">
                <label for="2nd-level">2nd level:</label>
                <input id="2nd-level" data-dojo-type="dijit/form/ValidationTextBox" required="true" data-dojo-props="value: at('rel:', 'prop2ndLevel')"/>
            </div>
        </form>
    </body>
</html>

希望这有帮助。

最好, - Akira