我有一个巴士模型,它有一个嵌套的路线模型。如果子模型无效,防止保存?
class Busables.Models.Bus extends Backbone.Model
url: '/buses'
defaults:
route: new Busables.Models.Route()
然后我有一个监听总线模型的error
活动的总误差的看法,他的路线。
class Busables.Views.Errors extends Backbone.View
el: '#error-area'
template: JST['shared/error']
# model in this instance is a bus
initialize: ->
@model.get('route').bind 'error', this.routeError, this
@model.bind 'error', this.busError, this
所以,当用户提交页面上的表单,我省公交车的属性,并将其发送到服务器:
class Busables.Views.NewBus extends Backbone.View
...
saveBus: (event) ->
event.preventDefault()
@model.save {
seats: this.$('#bus_seats').val(),
}, {
success: (model, response) ->
console.log "Success. The model and response: ", model, response
window.location.href = "/buses/#{response.id}"
}
所以现在的问题是,我怎么触发在保存公交车时嵌套路线的验证?
是的。我认为这将是前进的方向。问题是我已经相当广泛地使用了[Backbone Validations](https://github.com/n-time/backbone.validations)插件。我想我只是看看如何处理。 –