YUI3 Y.Attribute

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<script src="http://yui.yahooapis.com/3.16.0/build/yui/yui-min.js"></script>
<script type="text/javascript">

    YUI.add('electron', function(Y) {

        var REST_ENERGY = 511.00;

        function Electron(config) {

            this.addAttrs({
                charge: {
                    value: -1,
                    readOnly: true
                },
                energy: {
                    value: REST_ENERGY,
                    validator: function(en) {
                        return (en >= REST_ENERGY);
                    }
                }
            }, config);

        }
        Y.Electron = Y.augment(Electron, Y.Attribute);
    }, '0.0.1', {requires: ['attribute']});

    YUI().use('electron', function(Y) {

        var e1 = new Y.Electron({charge: 10, energy: 311});
        Y.log("charge: " + e1.get('charge'));
        Y.log("energy: " + e1.get('energy'));


        var e2 = new Y.Electron();
        e2.on('chargeChange', function(ev) {
            Y.log("charge on");
        });

        e2.after('chargeChange', function(ev) {
            Y.log("charge after");
        });

        e2.on('energyChange', function(ev) {
            Y.log("energy on");
            Y.log(ev.prevVal + " ------- " + ev.newVal);
        });

        e2.after('energyChange', function(ev) {
            Y.log("energy after");
        });

        e2.set('charge', 123);
        e2.set('energy', 1003);
    });
</script>
</body>
</html>

 

posted @ 2014-06-13 16:06  wheats  阅读(178)  评论(0编辑  收藏  举报