diff --git a/src/core/4.widget.js b/src/core/4.widget.js index 4a1212962..5de5d7c85 100644 --- a/src/core/4.widget.js +++ b/src/core/4.widget.js @@ -784,33 +784,34 @@ }; BI.watch = function (vm, watch, handler) { - if (vm instanceof BI.Model) { - var watchers = []; - if (BI.isKey(watch)) { - var k = watch; - watch = {}; - watch[k] = handler; - } - for (var key in watch) { - var innerHandler = watch[key]; - if (BI.isArray(handler)) { - for (var i = 0; i < handler.length; i++) { + // 必须要保证组件当前环境存在 + if (BI.Widget.current) { + if (vm instanceof BI.Model) { + var watchers = []; + if (BI.isKey(watch)) { + var k = watch; + watch = {}; + watch[k] = handler; + } + for (var key in watch) { + var innerHandler = watch[key]; + if (BI.isArray(handler)) { + for (var i = 0; i < handler.length; i++) { + watchers.push(Fix.watch(vm.model, key, innerHandler, { + store: vm + })); + } + } else { watchers.push(Fix.watch(vm.model, key, innerHandler, { store: vm })); } - } else { - watchers.push(Fix.watch(vm.model, key, innerHandler, { - store: vm - })); } + // vm中一定有_widget + BI.Widget.current._watchers || (BI.Widget.current._watchers = []); + BI.Widget.current._watchers = BI.Widget.current._watchers.concat(watchers); + return; } - // vm中一定有_widget - vm._widget._watchers || (vm._widget._watchers = []); - vm._widget._watchers = vm._widget._watchers.concat(watchers); - return; - } - if (BI.Widget.current) { handler = watch; watch = vm; BI.Widget.current.$watchDelayCallbacks || (BI.Widget.current.$watchDelayCallbacks = []);