Skip to content

Instantly share code, notes, and snippets.

@140am
Created March 30, 2012 07:55
Show Gist options
  • Save 140am/2249117 to your computer and use it in GitHub Desktop.
Save 140am/2249117 to your computer and use it in GitHub Desktop.
backbone.js - dynamically set a model attributes based on From selection
class exports.AddTaskTemplateView extends Backbone.View
events:
"change input": "fieldChanged"
"change select": "fieldChanged"
'click #btn-submit' : 'submitInput'
render: (context = {}) ->
$(@el).html require('./templates/user_create') context
this
validateForm: ->
{FlashWindowView} = require 'views/misc_view'
# input validation
template_name = $('input[name=name]').val()
if not template_name
console.warn 'get templaten mae'
flash = new FlashWindowView
flash.render message: 'Please enter a Name first'
return no
template_function = $('select[name=function]').val()
if not template_function? or template_function == 'Select..'
flash = new FlashWindowView
flash.render message: 'Please enter a Function to use first'
return no
return yes
parseForm: ->
@model.set 'function', $('select[name=function]').val()
fieldChanged: (e) ->
field = $(e.currentTarget)
if (field.get(0).tagName == 'SELECT')
value = $("option:selected", field).val()
else
value = field.val()
data = {}
data[field.attr('id')] = value
console.log('selectionChanged: ' + field.attr('name') + ' : ' + value)
@model.set field.attr('name'), value
submitInput: (e) ->
if @validateForm() and @parseForm()
@trigger 'template.create'
return false
@alexus85
Copy link

alexus85 commented Aug 5, 2015

thanks for the headache

@140am
Copy link
Author

140am commented Sep 23, 2015

ha wish github would finally implement comment notifications @alexus85 - hit me up if you need a hand with backbone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment