|
|
@ -7,27 +7,39 @@ |
|
|
|
})); |
|
|
|
})); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var models = {}; |
|
|
|
var models = {}, watches = {}; |
|
|
|
addEventListener("message", function (e) { |
|
|
|
addEventListener("message", function (e) { |
|
|
|
var data = e.data; |
|
|
|
var data = e.data; |
|
|
|
switch (data.eventType) { |
|
|
|
switch (data.eventType) { |
|
|
|
case "action": |
|
|
|
case "action": |
|
|
|
models[data.name][data.action].apply(models[data.name], data.args); |
|
|
|
models[data.name][data.action].apply(models[data.name], data.args); |
|
|
|
break; |
|
|
|
break; |
|
|
|
default: |
|
|
|
case "destroy": |
|
|
|
|
|
|
|
BI.each(watches[data.name], function (i, unwatches) { |
|
|
|
|
|
|
|
unwatches = BI.isArray(unwatches) ? unwatches : [unwatches]; |
|
|
|
|
|
|
|
BI.each(unwatches, function (j, unwatch) { |
|
|
|
|
|
|
|
unwatch(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
delete models[data.name]; |
|
|
|
|
|
|
|
delete watches[data.name]; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case "create": |
|
|
|
var store = models[data.name] = BI.Models.getModel(data.type, data.options); |
|
|
|
var store = models[data.name] = BI.Models.getModel(data.type, data.options); |
|
|
|
for (var i = 0, len = data.watches.length; i < len; i++) { |
|
|
|
watches[data.name] = []; |
|
|
|
var key = data.watches[i]; |
|
|
|
BI.each(data.watches, function (i, key) { |
|
|
|
createWatcher(store.model, key, function () { |
|
|
|
watches[data.name].push(createWatcher(store.model, key, function (newValue, oldValue) { |
|
|
|
postMessage(BI.extend({}, data, { |
|
|
|
postMessage(BI.extend({}, data, { |
|
|
|
eventType: "watch", |
|
|
|
eventType: "watch", |
|
|
|
currentWatchType: key |
|
|
|
currentWatchType: key |
|
|
|
}, {args: [].slice.call(arguments, 0, 2)})); |
|
|
|
}, {args: [newValue, oldValue]})); |
|
|
|
|
|
|
|
})); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
postMessage(BI.extend({}, data, { |
|
|
|
postMessage(BI.extend({ |
|
|
|
|
|
|
|
eventType: "create" |
|
|
|
eventType: "create" |
|
|
|
}, data, {msg: store.model})); |
|
|
|
}, {msg: store.model})); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
}, false); |
|
|
|
}, false); |
|
|
|