kendo要求从另一个模块访问视图模型

kendo requirejs access viewmodel from another module

本文关键字:访问 视图 模型 模块 另一个 kendo      更新时间:2023-09-26

如何从另一个requirejs模块"获取"answers"设置"一个视图模式的属性?

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)?

您不能按照现在的方式执行。对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};
});