DOJO dijit / registry registry.byId()不看小部件?

时间:2014-12-13 15:57:12

标签: javascript dojo

我需要观看registry.byId(http://dojotoolkit.org/reference-guide/1.10/dijit/registry.html)返回的小部件。

我可以从registry.byId获取引用,但是看看它的属性不起作用。 知道如何解决这个问题吗?

http://jsfiddle.net/5rpjby7j/

    <span data-dojo-type="BusinessCard"></span>
    <span data-dojo-type="BusinessCard"></span>


 window.State = {
        name: 'state: name',
        surname: 'state: surname',
        custom: 'state: custom',
        custom2: 'state: custom2'
    };

    window.FactoryProperties = function () {
        // here you can add private members (closure)
        // return an object with common properties for wpis
        return {
            custom: window.State.custom, // data taken dynamically from state
            _setCustomAttr: function (value) {
                this.custom = value;
                window.State.custom = value;
                this.customNode.innerHTML = window.State.custom;
            },
            custom2: window.State.custom2,
            _setCustom2Attr: function (value) {
                this.custom2 = value;
                window.State.custom2 = value;
                this.customNode2.innerHTML = window.State.custom2;
            }
        };
    };

    require([
        "dojo/_base/declare", "dojo/parser", "dojo/ready",
        "dijit/_WidgetBase", "dijit/_TemplatedMixin", "dojo/_base/lang", "dijit/registry"
    ], function (declare, parser, ready, _WidgetBase, _TemplatedMixin, lang, registry) {
        // properties custom for wpi
        var prop1 = {
            templateString: "<div class='businessCard'>" +
                "<div>NAME  : <span data-dojo-attach-point='nameNode'></span></div>" +
                "<div>SURNAME : <span data-dojo-attach-point='surnameNode'></span></div>" +
                "<div>CUSTOM: <span data-dojo-attach-point='customNode'></span></div>" +
                "<div>CUSTOM2: <span data-dojo-attach-point='customNode2'></span></div>" +
                "<br>" +
                "</div>",

            name: window.State.name,
            _setNameAttr: function (value) {
                window.State.name = value;
                this.name = value;
                this.nameNode.innerHTML = window.State.name;
            },

            surname: window.State.name,
            _setSurnameAttr: function (value) {
                window.State.surname = value;
                this.surname = value;
                this.surnameNode.innerHTML = window.State.surname;
            }
        };
        var propTot = lang.mixin(prop1, window.FactoryProperties());
        declare("BusinessCard", [_WidgetBase, _TemplatedMixin], propTot);

        ready(function () {
            parser.parse();
            // watch a widget PROBLEM HERE
            var mywidget = registry.byId("BusinessCard_1");
            mywidget.watch("name", function () {
                alert('name have been updated!!!!!!!!!!!');
            });

            registry.byId("BusinessCard_1").set("name", 'MODIFIED name from code');
            registry.byId("BusinessCard_1").set("custom", 'MODIFIED surname from code');
            //registry.byId("BusinessCard_1").set("custom2", 'CUSTOM2 MODIFIED PROGRAMMATICALLY');
            console.log(registry.toArray()); // check the registry
            console.log(registry.byId("BusinessCard_1"));

        });
    });

1 个答案:

答案 0 :(得分:1)

修复了这里 http://jsfiddle.net/5rpjby7j/1/

使用this._set("surname", value)

中的_setSurnameAttr()解决
            _setSurnameAttr: function (value) {
                window.State.surname = value;
                this._set("surname", value)
                this.surnameNode.innerHTML = window.State.surname;
            }

有用的帖子 http://dojo-toolkit.33424.n3.nabble.com/watch-on-custom-widget-not-firing-td3997554.html