Backbone.js 集合的客户端验证
我正在使用backbone-on-rails gem 并得到以下信息:
模型
class Crayons.Models.Color extends Backbone.Model
validate: (attrs) ->
if attrs.colorname.length == 0
return "colorname can't be empty"
集合
class Crayons.Collections.Color extends Backbone.Collection
url: '/api/colors'
路由器方法中创建的
initialize: ->
@collection = new Crayons.Collections.Color()
@collection.fetch()
集合是在 ColorIndex 中的
createCrayonId: (event) ->
event.preventDefault()
val = new Crayons.Models.Color()
val.on("error", @errorHandling);
val.set(colorname: $('#new_color_name').val())
@collection.create colorid: val.get("colorname");
$('#new_color')[0].reset()
errorHandling: (model, event) ->
alert(event)
我有一个简单的验证,当长度为零时显示警报。但是,在警报框之后,由于 @collection.create colorid: val.get("colorname");
,项目仍然被添加到集合中,
我该怎么做才能阻止集合添加当错误被捕获时,结果是什么?
更新1 短期解决方案如下,它有效,但我不确定它是否是正确的做事方式
ok = val.set(colorname: $('#new_video_url').val())
if ok
@collection.create colorname: val.get("colorname");
Update2
class Crayons.Collections.Colors extends Backbone.Collection
url: '/api/videos'
model: new Crayons.Models.Color()
validate: (attrs) ->
if attrs.colorname.length == 0
return "colorname can't be empty"
正确的方式 验证函数应该位于模型中,并且警告错误的函数应该在模型上初始化。
class Crayons.Models.Color extends Backbone.Model
initialize: ->
this.bind("error", @errorHandling)
errorHandling: (model, event) ->
alert(event)
validate: (attrs) ->
#your error validation goes here
现在索引中的方法非常简单
createCrayonId: (event) ->
event.preventDefault()
@collection.create colorname: $('#new_color_name').val()
I'm using backbone-on-rails gem and got the following:
Model
class Crayons.Models.Color extends Backbone.Model
validate: (attrs) ->
if attrs.colorname.length == 0
return "colorname can't be empty"
Collection
class Crayons.Collections.Color extends Backbone.Collection
url: '/api/colors'
collection is created in a router
initialize: ->
@collection = new Crayons.Collections.Color()
@collection.fetch()
Methods in ColorIndex
createCrayonId: (event) ->
event.preventDefault()
val = new Crayons.Models.Color()
val.on("error", @errorHandling);
val.set(colorname: $('#new_color_name').val())
@collection.create colorid: val.get("colorname");
$('#new_color')[0].reset()
errorHandling: (model, event) ->
alert(event)
I have a simple validation that shows an alert when the length is zero. However, after the alert box, the items are still being added to the collection because of @collection.create colorid: val.get("colorname");
What can I do to stop the collection from adding the results in it when an error has been caught?
Update1
Short term solution is below and it works but I'm unsure if its a correct way of doing things
ok = val.set(colorname: $('#new_video_url').val())
if ok
@collection.create colorname: val.get("colorname");
Update2
class Crayons.Collections.Colors extends Backbone.Collection
url: '/api/videos'
model: new Crayons.Models.Color()
validate: (attrs) ->
if attrs.colorname.length == 0
return "colorname can't be empty"
Correct way
The validate function should be in the model and function that alerts the error should be initialized on the model.
class Crayons.Models.Color extends Backbone.Model
initialize: ->
this.bind("error", @errorHandling)
errorHandling: (model, event) ->
alert(event)
validate: (attrs) ->
#your error validation goes here
Now the method in index is quite simple
createCrayonId: (event) ->
event.preventDefault()
@collection.create colorname: $('#new_color_name').val()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的验证会触发颜色模型,但不会触发集合模型。因此,如果您想以干净的方式执行此操作,则应该为集合模型添加验证以检查 colorId 是否有效,与 videoid 的更新相同。
您可以设置集合的模型属性,并且在创建/保存集合模型时,它将使用该模型的验证来确定是否应该创建/保存模型
Your validation triggers for the color model but not for the collection model. So if you wanted to do it the clean way you should add a validation for the collection model to check if colorId is valid, same in the update with videoid.
You can set a model property of a collection and the when creating/saving a collection model it will use this model's validation to determine if it should create/save the model or not