class IuguUI.View extends IuguUI.Base
layout: "iugu-ui-view"
secondaryView: false
initialize: ->
super
window.Events.on "fillSlots", @fillSlots
window.Events.on "resetSlots", @resetSlots
@secondaryView = @options.secondaryView if @options.secondaryView
if @model
Backbone.Validation.bind @,
forceUpdate: true
valid: (view, attr, selector) ->
view.valid view, attr, selector
invalid: (view, attr, error, selector) ->
view.invalid view, attr, error, selector
@model.on 'error', @addErrors, @
fillSlots: ( context ) ->
_.each( _.keys(context)
( key ) ->
renderContext = context[key]
if renderContext.el
context[key].$el.detach()
@$(key).empty().append context[key].el
else
@$(key).empty().append context[key]
)
resetSlots: ( slots ) ->
_.each( slots,
( slot ) ->
@$(slot).empty()
)
enableLoader: ->
debug "ENABLED LOADER"
@viewLoader = $('
')
$(@el).append @viewLoader
disableLoader: ->
debug "DISABLED LOADER"
if @viewLoader
@viewLoader.remove()
load: ->
debug "ON LOAD"
@disableLoader()
@render()
valid: (view, attr, selector) ->
control = view.$ '[' + selector + '=\"' + attr + '\"]'
group = control.parents ".form-group"
ctrlGroup = control.parents ".control-group"
list = group.find ".error-list"
return if view.model.preValidate attr, control.val()
list.find(".error-" + attr).remove()
ctrlGroup.removeClass "error"
list.parent().remove() if list.find(".error").length == 0
invalid: (view, attr, error, selector) ->
control = view.$ '[' + selector + '=\"' + attr + '\"]'
group = control.parents ".form-group"
ctrlGroup = control.parents ".control-group"
list = group.find ".error-list"
ctrlGroup.addClass "error"
if list.length == 0
group.prepend ''
list = group.find ".error-list"
list.find(".error-" + attr).remove()
if _.isArray(error)
_.each(error, (err) ->
list.append '' + attr + ' ' + err + ''
)
else
list.append '' + error + ''
addErrors: (model, errors) ->
invalid = @invalid
view = @
errors_json = JSON.parse errors.responseText
_.each(errors_json.errors, (val, key) ->
invalid view, key, val, "name"
)
render: ->
rivets.bind this.$el, {model: @model} if @model
if app.activeView != @ and @secondaryView == false
app.activeView.close() if app.activeView
app.activeView = @
if window.app.rootWindow.setTitle
window.app.rootWindow.setTitle @title
super
@
unload: () ->
debug 'Called IuguUI.View:unload'
if @model
Backbone.Validation.unbind @
@model.off null, null, @
if @collection
@collection.off null, null, @
super
@IuguUI.View = IuguUI.View