Created
March 30, 2012 07:55
-
-
Save 140am/2249117 to your computer and use it in GitHub Desktop.
backbone.js - dynamically set a model attributes based on From selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ha wish github would finally implement comment notifications @alexus85 - hit me up if you need a hand with backbone!