Backbone.js的触发时更改值不会改变 [英] Trigger backbone.js change when value doesn't change

查看:117
本文介绍了Backbone.js的触发时更改值不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Backbone.js的已知功能,当你设置没有改变也不会触发change事件数据,也不会通过验证。然而,我所需要的变革触发事件,因为我存储从一个AJAX调用存储后端验证结果的JSON响应。如果用户保持提交表单,同时使同一字段为空,后端验证将返回相同的JSON结果,当我将它保存到模型不会触发change事件。

It's a known feature of backbone.js that when you set data that hasn't changed it won't fire the change event, nor will it go through validations. I however need the change event to fire as I'm storing a JSON response from an AJAX call which stores results of backend validation. If the user keeps submitting the form while leaving the same field empty, the backend validation will return the same JSON result and when I save it to the model it won't trigger the change event.

有几件事情我已经在那里我设定的数据导入模型中的AJAX成功回调中尝试:

A few things I've tried within the AJAX success callback where I set the data into the model:

t.model.unset('fieldErrors',{silent: true});
t.model.set({fieldErrors: JSONResponse});

尝试性解决方案#2

t.model.set({fieldErrors: null},{silent: true});
t.model.set({fieldErrors: JSONResponse});

无论这些结果在变化事件触发第二次当调用时,用户有相同的JSONResponse。

Neither of these results in the change event firing a second time when the call is made and the user has the same JSONResponse.

推荐答案

手动触发更改事件:

t.model.trigger('change', t.model);

t.model.trigger('change:fieldErrors', t.model, newFieldErrorsValue);

这篇关于Backbone.js的触发时更改值不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆