1

I have this code but when I clicked the Button Test , don't enter the function modelChanged in my App,only enter when de model was create the code is in this link: http://jsfiddle.net/bs38A/16/

enyo.kind({
    name: "App",
    bindings: [
        {from: ".model.foo", to: ".$.notFoo.content"}
    ],
    components: [
        {classes: "onyx-toolbar-inline", components:[
            {content: "model's 'foo' property:"},
            {name: "notFoo"}
        ]},
        {kind: "onyx.Button", content: "Test", ontap: "test"}
    ],
    test: function() {
        var m = this.model;
        m.set("foo", "model");
    },
    modelChanged: function(){
        this.inherited(arguments);
        console.log("Change Model");
    }
});
enyo.kind({
    name: "MyModel",
    kind: "enyo.Model"
});
var app = new App().renderInto(document.body);
var model = new MyModel({foo: "bar"});
app.set("model", model);
manu
  • 13
  • 3

2 Answers2

1

Your code currently only fires once (when you do app.set("model", model);. Because changing a property on the model doesn't actually change the model property of App, the modelChanged event isn't fired.

If you want to be notified whenever any property of the model changes you'll want to use a different method. What you want to look at is the documentation for the Event Emitter mixin: http://enyojs.com/docs/latest/#/mixin/enyo.EventEmitter

You'll want to do something like this:

create: function() {
    this.inherited(arguments);
    this.model.on("*", this.modelUpdated, this);
},
modelUpdated: function(sender, e, props) {
    this.log("Model was updated");
}

That will monitor for any changes. You may want to get more specific, such as using "change" instead of "*". If you want to monitor changes to specific properties on a model then you will want to subclass Model and put a change handler in the model.

Pre101
  • 2,370
  • 16
  • 23
0

this is because the modelChanged() function is executed when the model property of App changes/is set. Since enyo.Model isn't an enyo.Component, it doesn't get the automatic propertyChanged functionality. Check on how to add observers here http://enyojs.com/docs/latest/developer-guide/building-apps/managing-data/building-data-driven-apps.html#observers

Webby Vanderhack
  • 889
  • 7
  • 13