我正在學習Enyo js框架中的事件,並且無法理解爲什麼我得到的錯誤和事件onModelChanged未處理。錯誤消息是 「遺漏的類型錯誤:未定義是不是一個函數」簡單示例中未處理和未定義Enyo自定義事件
代碼:
debugger;
enyo.kind({
name : "MyModel",
kind: "enyo.Model",
defaultSource: "mocked",
published: {
title : "not set",
text : "not set",
},
events : {
onModelChanged : "",
},
handlers: {
onModelChanged : "modelLoadedHandler"
},
modelLoadedHandler : function(inSender, inEvent){
debugger;
console.log("handler in model");
console.log(inEvent.textMsg);
return true;
},
fetch : function(opts){
this.inherited(arguments);
debugger;
this.doModelChanged({textMsg: "fire event"}); // Uncaught TypeError: undefined is not a function
}
});
var model = new MyModel();
model.fetch();
附:保持此代碼作爲一個答案不通過的jsfiddle
enyo.kind({
name: "MyMockSource",
kind: "enyo.Source",
fetch: function(model, opts) {
if(model instanceof enyo.Model) {
opts.success({title: "testing", text: "Some stuff"});
} else {
throw new Error("Model mock only");
}
}
});
new MyMockSource({name: "mocked"});
enyo.kind({
name : "MyModel",
kind: "enyo.Model",
source: "mocked",
attributes: {
title : "not set",
text : "not set",
},
fetched: function(opts){
this.inherited(arguments);
enyo.log("fetched");
// Do something here if you need to, otherwise you might want to overload
// the parse method and set parse: true to modify the retrieved data
},
titleChanged: function(was, is) {
enyo.log("Title is now: " + is);
}
});
enyo.kind({
name: "MyView",
components: [
{kind: "Button", content: "Fetch", ontap: "fetch"},
{name: "title"},
{name: "text"}
],
bindings: [
{from: "model.title", to: "$.title.content"},
{from: "model.text", to: "$.text.content"}
],
fetch: function() {
this.set("model", new MyModel()); // Probably want to create model elsewhere
this.model.on("change", enyo.bindSafely(this, "modelChanged"));
this.model.fetch();
},
modelChanged: function() {
this.log("Something happened to my model!");
}
});
new enyo.Application({ name: "app", view: "MyView" });
對不起小提琴,我在JS和UI新,並沒有抓到你的意思之外的事件是什麼。但如何解決這個代碼,使我按我期望的方式工作嗎? –
我的意思是說,如果你想從一個不是模型本身的對象中從模型中捕捉事件。增加了工作小提琴。 – Pre101