简体   繁体   English

在下拉菜单中存储价值-Ember.js

[英]Storing value in drop-down - Ember.js

I've never worked with Ember before, and am fairly new to JavaScript, and have for the past couple of days been trying to translate some HTML and JS into the Ember framework (I got some help from a colleague). 我以前从未使用过Ember,并且对JavaScript还是很陌生,并且在过去的几天里,我一直在尝试将一些HTML和JS转换为Ember框架(我从同事那里得到了一些帮助)。 What I've tried to accomplish is, first; 首先,我试图做到的是: getting Google Geocoder to function and fill some text-feild with latitude/longitude from an inputted address, and second; 使Google Geocoder正常运行,并从输入的地址中用纬度/经度填充一些文本字段,其次; filling some text-field with a specific email address based on what selection the user made from a drop-down menu. 根据用户从下拉菜单中进行的选择,使用特定的电子邮件地址填充某些文本字段。 Everything is working quite good, except that when I select an option from the drop-down it sometimes displays another selection (for example, selecting "Oakland" displays "Butte" ) - guessing it has something to do with "value", just not sure how to address and fix the issue. 一切工作正常,只是当我从下拉菜单中选择一个选项时,它有时会显示另一个选择(例如, 选择“ Oakland”时显示“ Butte” )-猜测它与“ value”有关,只是确定如何解决和解决此问题。 Does anyone have any idea on a fix, or perhaps another approach to make it display the correct selection, while still sending the correct email-address to the text-field? 是否有人对修复有任何想法,或者有其他方法可以使它显示正确的选择,同时仍将正确的电子邮件地址发送到文本字段?

Link to an Ember Twiddle : MyProject 链接到Ember TwiddleMyProject

Also, separate question - I'm also not sure how my "dataValue"-variable works (routes/demo.js); 另外,还有一个单独的问题-我也不确定我的“ dataValue”变量如何工作(routes / demo.js); my colleague helped me out with that part and I'm just a little unsure of my "setEmail"-function knows what dataValue is supposed to be as I can't figure out where the variable is set. 我的同事帮了我这部分,我对我的“ setEmail”功能有点不确定,因为我不知道变量的设置位置,所以知道应该是什么dataValue。 If someone could clear that up for me, I'd very much appreciate it. 如果有人可以帮我解决这个问题,我将非常感激。

I appreciate any suggestions and insight! 我感谢任何建议和见解! Thank you. 谢谢。

Your problem is that; 你的问题是; various options you create share the exact same "value". 您创建的各种选项共享完全相同的“值”。 To explain; 解释; whenever you select an option from the select; 每当您从选择中选择一个选项时; all the x-option components recalculates their "selected" computed properties in case the value of x-select changes. 如果x-select的值发生更改,则所有x-option组件都会重新计算其“选定的”计算属性。 I have modified your twiddle and just inserted some console logs to x-select and x-option in order to explain you the situation. 我已经修改了您的方法,只是向x-select和x-option插入了一些控制台日志,以向您解释这种情况。 See the modified twiddle . 参见修改后的旋转

Initially select Oakland and open the console and trace it. 最初选择Oakland并打开控制台并进行跟踪。 As you see; 正如你看到的; x-select first updates the value to 1; x-select首先将值更新为1; and all counties with value equal to 1 (Oakland, Collier, etc.) outputs their text since they are all selected. 并且值均等于1的所有县(奥克兰,科利尔等)都将输出其文本,因为它们均已选中。 Since Butte is the last option in list with value 1; 由于Butte是列表中值为1的最后一个选项; its text is displayed in the user interface. 其文本显示在用户界面中。 Now just select; 现在选择 Brevard and as expected St. Lucie is displayed in the user interface but all options with value 3 are selected including Brevard and Indian River. 在用户界面中显示了Brevard和St. Lucie的预期效果,但是选择了所有值为3的选项,包括Brevard和Indian River。 Now just select Indian River; 现在选择印度河; this time x-select logs again; 这次x-select再次记录日志; but x-options do not log; 但是x选项不会记录; because the x-select's value did not change (it was 3; and it is still 3 since an option with the same value is selected). 因为x-select的值没有改变(为3;由于选择了具有相同值的选项,所以仍为3)。

So to sum up; 总结一下; in this implementation you need to give unique option values to every single county. 在此实现中,您需要为每个县赋予唯一的选项值。 Otherwise; 除此以外; the county that has the largest index inside prepopulatedCounties array will be selected among the counties sharing same value. 在具有相同值的县中,将选择prepopulatedCounties数组中索引最大的县。 If you continue selecting another county that has the same value as the last selected one; 如果继续选择与上一个选定县具有相同值的另一个县; you will think your code is working mistakenly because select will display it correctly; 您会认为您的代码工作不正确,因为select可以正确显示它; however that is not the case! 但是事实并非如此!

If you intent to use select in ember; 如果您打算使用,请在ember中选择; I would highly recommend you to use ember-power-select instead of reinventing the wheel. 我强烈建议您使用ember-power-select而不是重新发明轮子。

Regarding your second question; 关于第二个问题; it is related with actions. 它与行动有关。 You send an event to router via this.sendAction('action', this.get('value'), this); 您通过this.sendAction('action', this.get('value'), this);向路由器发送事件this.sendAction('action', this.get('value'), this); inside x-select and you handle this inside demo.js router because you declared setEmail as the event handler inside demo.hbs with {{#x-select value=model.selectedCounty action='setEmail'}} the last part. 在x-select内部,您可以在demo.js路由器内部进行处理,因为您在最后一部分中将{{#x-select value=model.selectedCounty action='setEmail'}}声明为setEmail作为demo.hbs内部的事件处理程序。 I highly recommend you to look at Emberjs official guide for action registration and handling. 我强烈建议您参阅Emberjs官方指南进行操作注册和处理。 Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM