Created
August 20, 2012 22:43
-
-
Save michaeldeol/3408744 to your computer and use it in GitHub Desktop.
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
// View: NewWineView | |
window.WineNewView = Backbone.View.extend({ | |
template: _.template( $('#tpl-wine-new').html() ), | |
events: { | |
'change input': 'wineChange', | |
'click .save': 'wineSave', | |
'click .cancel': 'wineCancel' | |
}, | |
initialize: function () { | |
console.log( 'WineNewView Created: ', this ); | |
}, | |
render: function () { | |
this.$el.html( this.template(this.model.toJSON()) ); | |
return this; | |
}, | |
wineChange: function () { | |
console.log( 'changed' ); | |
}, | |
wineSave: function () { | |
this.model.set({ | |
_id: null, // This seems hacky | |
name: this.$('#name').val(), | |
grapes: this.$('#grapes').val(), | |
country: this.$('#country').val(), | |
region: this.$('#region').val(), | |
year: this.$('#year').val(), | |
description: this.$('#description').val() | |
}); | |
if ( this.model.isNew() ) { | |
console.log( this.model ); | |
var self = this; | |
app.wineList.create(this.model, { | |
succes: function () { | |
app.navigate( '/', { trigger: true, replace: true } ); | |
} | |
}); | |
} else { | |
this.model.save(); | |
} | |
return false; | |
}, | |
wineCancel: function () { | |
console.log( 'Canceled' ); | |
return false; | |
} | |
}); | |
// Header View | |
window.HeaderView = Backbone.View.extend({ | |
template: _.template( $( '#header' ).html() ), | |
events: { | |
'click .add': 'addWine', | |
'click .home': 'goHome' | |
}, | |
initialize: function () { | |
console.log( 'header loaded' ); | |
}, | |
render: function () { | |
this.$el.html( this.template() ); | |
return this; | |
}, | |
addWine: function () { | |
app.navigate( '/wines/add', { trigger: true } ); | |
return false; | |
}, | |
goHome: function () { | |
app.navigate( '', true ); | |
return false; | |
} | |
}); | |
var AppRouter = Backbone.Router.extend({ | |
routes: { | |
'': 'list', | |
'wines/add': 'wineAdd', | |
'wines/:id': 'wineDetails' | |
}, | |
initialize: function () { | |
// Init info | |
$( '#header' ).html( new HeaderView().render().el ); | |
}, | |
list: function () { | |
this.wineList = new WineCollection(); | |
this.wineListView = new WineListView({ model: this.wineList }); | |
this.wineTView = new WineView({ model: this.wineList }); | |
this.wineList.fetch(); | |
$( '#side-bar' ).html( this.wineListView.render().el ); | |
$( '#wines-thumbs' ).html( this.wineTView.render().el ); | |
}, | |
wineAdd: function () { | |
var wine = new Wine(); | |
$( '#wines-thumbs' ).html( new WineNewView({ model: wine }).render().el ); | |
}, | |
wineDetails: function ( id ) { | |
this.wine = this.wineList.get( id ); | |
if ( app.wineView ) app.wineView.close(); | |
this.wineView = new WineView({ model: this.wine }); | |
$( '#wines-thumbs' ).html( this.wineView.render().el ); | |
} | |
}); | |
var app = new AppRouter(); | |
Backbone.history.start({ pushState: true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Change your routes from:
To: