kendo requirejs从另一个模块访问viewmodel

时间:2014-04-16 15:15:26

标签: javascript kendo-ui requirejs

如何获得'和'设置'来自另一个requirejs模块的一个视图模式的属性?

test.js
================
define(['kendo'],
   function (kendo) {
       var vm = kendo.observable({
           propertyA: "a"
       });
   return {vm: vm};
});

another.js
================
define(['kendo'],
   function (kendo) {
       var testMethod = function () {
           var test = require(['test']);
           var testName = test.vm.get("propertyA");      //<< uncaught typeerror ???
           test.vm.set("propertyA", "b");                //<< uncaught typeerror ???
       };
   return {testMethod: testMethod};
});

我很抱歉,因为我有一个c#背景,除非我当前的项目,否则不习惯使用js。

我应该向test.js vm添加方法来获取和设置viewModel的属性,还是我可以通过另一种方式直接从另一个模块获取和设置属性(此示例中为propertyA)?

1 个答案:

答案 0 :(得分:0)

你无法按照自己的方式去做。您对require的来电是异步。因此test不具备您想要的价值。

你可以这样做:

define(['kendo', 'test'], function (kendo, test) {
       var testMethod = function () {
           var testName = test.vm.get("propertyA");      //<< uncaught typeerror ???
           test.vm.set("propertyA", "b");                //<< uncaught typeerror ???
       };
   return {testMethod: testMethod};
});