CoffeeScript + KnockoutJS 函数绑定

发布于 2024-12-07 16:56:30 字数 690 浏览 0 评论 0原文

我正在使用 CoffeeScript 和 KnockoutJS,但在从函数内获取视图模型的值时遇到问题。

我有一个视图模型:

window.Application || = {}
class Application.ViewModel 
    thisRef = this
    searchTerm: ko.observable("")
    search: ->
        alert @searchTerm

编译为:

window.Application || (window.Application = {});
Application.ViewModel = (function() {
  var thisRef;
  function ViewModel() {}
  thisRef = ViewModel;
  ViewModel.prototype.searchTerm = ko.observable("");
  ViewModel.prototype.search = function() {
    return alert(this.searchTerm);
  };
  return ViewModel;
})();

该视图模型是父视图模型的一部分,它将其公开为字段。问题是我无法获得对子视图模型的引用。在搜索函数中,“this”是父级的实例,这是我不想要的。

I'm using CoffeeScript and KnockoutJS and have a problem getting the values of my view model from within a function.

I have a view model:

window.Application || = {}
class Application.ViewModel 
    thisRef = this
    searchTerm: ko.observable("")
    search: ->
        alert @searchTerm

Which compiles to:

window.Application || (window.Application = {});
Application.ViewModel = (function() {
  var thisRef;
  function ViewModel() {}
  thisRef = ViewModel;
  ViewModel.prototype.searchTerm = ko.observable("");
  ViewModel.prototype.search = function() {
    return alert(this.searchTerm);
  };
  return ViewModel;
})();

This view model is part of a parent view model which exposes it as field. The problem is that I can't get a reference to the child view model. In the search function 'this' is a instance of the parent, which I don't want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

活雷疯 2024-12-14 16:56:31

搜索函数中,“this”是父级的实例...

这取决于您如何调用它。如果你

m = new Application.ViewModel
m.search()

这样做,那么this将是m;如果你

obj = {search: m.search}
obj.search()

这样写,那么 this 将是 obj

无论如何,只需使用 CoffeeScript 的 => 运算符即可:

search: =>
    alert @searchTerm

这样,search 中的 this/@ 将指向ViewModel 实例。

正如 Travis 所说,thisRef 将仅指向类,而不是实例。

In the search function 'this' is a instance of the parent...

That depends on how you call it. If you do

m = new Application.ViewModel
m.search()

then this will be m; if you write

obj = {search: m.search}
obj.search()

then this will be obj.

Anyway, just use CoffeeScript's => operator:

search: =>
    alert @searchTerm

That way, this/@ within search will point to the ViewModel instance.

thisRef will, as Travis says, just point to the class, not the instance.

乖乖 2024-12-14 16:56:31

您已经有一个 thisRef 对象,请使用 thisRef.searchTerm 而不是 @searchTerm。我在使用 jQuery 时经常遇到这种情况...

doSomething = ->
  target = $(@)
  $("#blah").click ->
    target.doSomethingElse()

因为 @doSomethingElse() 将绑定到执行单击的 DOM 元素。不是我想要的。

You already have a thisRef object hanging around, use thisRef.searchTerm instead of @searchTerm. I often have that happen when using jQuery...

doSomething = ->
  target = $(@)
  $("#blah").click ->
    target.doSomethingElse()

Since @doSomethingElse() would be bound to the DOM element the click was executed for. Not what I want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文