Created
September 10, 2012 19:35
-
-
Save ciberch/3693269 to your computer and use it in GitHub Desktop.
Activity Create Backbone View
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
var ActivityCreateView = Backbone.View.extend({ | |
el: '#new_activity', | |
initialize: function(){ | |
_.bindAll(this, 'newAct', 'render', 'changeType', 'includeLocation', 'sendMessage'); | |
this.trimForServer = App.helper.trimForServer; | |
var streamName = this.$el.find('#streamName').val(); | |
var verb = this.trimForServer(this.$el.find('#verb-show')); | |
var objectType = this.trimForServer(this.$el.find('#object-show')); | |
this.newAct(streamName, verb, objectType); | |
this.render(); | |
}, | |
events: { | |
"click .type-select" : "changeType", | |
"click #includeLocation" : "includeLocation", | |
"click #send-message" : "sendMessage" | |
}, | |
newAct : function(streamName, verb, objectType) { | |
this.streamName = streamName; | |
this.model = new Activity({ | |
object: {content: '', objectType: objectType, title: '', url: ''}, | |
verb: verb, | |
streams: [streamName] | |
}); | |
}, | |
render: function(){ | |
var actData = this.model.toJSON(); | |
this.$el.find("#specific-activity-input").html(jade.templates[actData.object.objectType]()); | |
return this; // for chainable calls, like .render().el | |
}, | |
changeType : function(event) { | |
console.log(event); | |
var itemName = $(event.target).data("type-show"); | |
if (itemName) { | |
$("#" + itemName)[0].innerHTML = event.target.text + " "; | |
var val = this.trimForServer(event.target.text); | |
if (itemName == "verb-show") { | |
this.model.set('verb', val); | |
} else { | |
var obj = this.model.get('object'); | |
obj.objectType = val; | |
this.model.set('object', obj); | |
} | |
} | |
this.render(); | |
}, | |
includeLocation : function(event) { | |
if (navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(App.helper.getLocation); | |
} else { | |
alert("Geo Location is not supported on your device"); | |
} | |
}, | |
sendMessage : function() { | |
console.log("In send message"); | |
var obj = this.model.get('object'); | |
obj.content = $("#msg").val(); | |
obj.url = $('#url').val(); | |
obj.title = $('#title').val(); | |
obj.objectType = this.trimForServer($('#object-show')); | |
this.model.set('object', obj); | |
var streamName = $('#streamName').val(); | |
this.model.set('streams', [streamName]); | |
var verb = this.trimForServer($('#verb-show')); | |
this.model.set('verb', verb); | |
if (this.model.isValid()) { | |
if (this.model.save()) { | |
this.newAct(streamName, verb, obj.objectType); | |
this.render(); | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment