gpt4 book ai didi

javascript - 如何将函数移动到 MVVM 中的单独文件?

转载 作者:行者123 更新时间:2023-12-02 17:44:15 30 4
gpt4 key购买 nike

我有以下 Kendo UI MVVM。这里的业务函数是写在viewModel内部的。我们如何将这些函数移动到不同的文件中?

我面临的挑战是由于这些函数中的“this”关键字,它引用了可观察对象中存在的模型。

查看模型

<script type="text/javascript">
var viewModel = kendo.observable({

// model definition
employees: [
{ name: "Lijo", age: "28" },
{ name: "Binu", age: "33" },
{ name: "Kiran", age: "29" }
],

personName: "",
personAge: "",

//Note: Business functions does not access any DOM elements using jquery.
//They are referring only the objects in the view model.

//business functions (uses "this" keyword - e.g. this.get("employees"))
addEmployee: function () {
this.get("employees").push({
name: this.get("personName"),
age: this.get("personAge")
});

this.set("personName", "");
this.set("personAge", "");
},

deleteEmployee: function (e) {

//person object is created using "e"
var person = e.data;

var employees = this.get("employees");
var index = employees.indexOf(person);
employees.splice(index, 1);
}

});

</script>

头部

<head>
<title>MVVM Test</title>

<script type="text/javascript" src="lib/kendo/js/jquery.min.js"></script>
<script type="text/javascript" src="lib/kendo/js/kendo.web.min.js"></script>

<!----Kendo Template-->
<script id="row-template" type="text/x-kendo-template">
<tr>
<td data-bind="text: name"></td>
<td data-bind="text: age"></td>
<td><button type="button" data-bind="click: deleteEmployee">Delete</button></td>
<td>
</tr>
</script>


<!--MVVM Wiring using Kendo Binding-->
<script type="text/javascript">

$(document).ready(function () {
kendo.bind($("body"), viewModel);
});

</script>

</head>

正文

<body>
<table>
<thead>
<tr>
<th>
Name
</th>
<th>
Age
</th>
</tr>
</thead>
<!--The data-template attribute tells Kendo UI that the employees objects should be formatted using a Kendo UI template. -->
<tbody data-template="row-template" data-bind="source: employees">
</tbody>
</table>
<br />
<br />
<form>
<input type="text" name="personName" placeholder="Name" data-bind="value: personName" /><br />
<input type="text" name="personAge" placeholder="Age" data-bind="value: personAge" /><br />
<button type="button" data-bind="click: addEmployee">
Add</button>
</form>
</body>

引用文献

  1. The Kendo MVVM Framework - packtpub
  2. MVVM / Remote binding -- demos.telerik
  3. DropDownList / MVVM - demos.telerik
  4. DataSource / Basic usage

最佳答案

无论您是在另一个 javascript 文件中还是在 kendo.observable 函数之外定义它,您只需将“this”绑定(bind)到该函数即可。

这未经测试,但第一个可能有效,而第二个肯定有效!

 function externalAddEmployee() {
this.get("employees").push({
name: this.get("personName"),
age: this.get("personAge")});
this.set("personName", "");
this.set("personAge", "");
}

kendo.observable({
// first solution: may not work because the 'this' you want is not properly defined
addEmployee: externalAddEmployee.bind(this);
// second solution: will work everytime
// the 'this' is properly defined, just proxy the call to the function
addEmployee: function() { return externalAddEmployee.apply(this, arguments); }

})

为了改进第二个解决方案,您甚至可以创建一个辅助函数:

function proxy(fn) { return function() { return fn.apply(this, arguments); })
// and then
kendo.observable({
addEmployee: proxy(externalAddEmployee)
})

关于javascript - 如何将函数移动到 MVVM 中的单独文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21906581/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com