gpt4 book ai didi

javascript - Ember,观察者以奇怪的顺序触发

转载 作者:行者123 更新时间:2023-11-29 15:42:54 25 4
gpt4 key购买 nike

这是我的 Controller EventTimezoneController。它的 content 属性设置为 Event 模型。

App.ChallengeTimezoneController = Ember.ObjectController.extend
timezones: [{value: "", label: ""}, {...}]

timezoneDidChange: (->
console.log "In controller", @get("timezone")
).observes("timezone") # I also tried "content.timezone"

现在我的 Event 模型:

App.Event = App.Challenge = DS.Model.extend(Ember.Validations,
timezone: DS.attr('string')

timezoneDidChange: (->
console.log "In model", @get("timezone")
).observes("timezone")
)

然后,我有一个 TimezoneSelect View

App.TimezoneSelect = Ember.Select.extend
valueBinding: "controller.timezone"
contentBinding: "controller.timezones"
optionValuePath: "content.value",
optionLabelPath: "content.label"

问题来了:当我在选择下拉列表中选择一个新值时,日志显示:

> In controller American Samoa
> In model American Samoa

为什么 Controller 中的方法 timezoneDidChange 在模型中的方法之前被调用,因为据我所知,它正在观察模型的属性?

最佳答案

ember.js 中, Controller 旨在代理 模型,因此首先在 Controller 上调用计算属性函数是有意义的。作为引用,您可以查看这个非常有用的信息 talk from Luke Melia at Ember.js NYC特别是at min 31:30其中显示了带有概念的幻灯片。

希望对你有帮助

关于javascript - Ember,观察者以奇怪的顺序触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16450077/

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