Skip to content

Instantly share code, notes, and snippets.

@michaeldeol
Created August 20, 2012 22:43
Show Gist options
  • Save michaeldeol/3408744 to your computer and use it in GitHub Desktop.
Save michaeldeol/3408744 to your computer and use it in GitHub Desktop.
// 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 });
@scottcorgan
Copy link

Change your routes from:

routes: {
    '': 'list',
    'wines/add': 'wineAdd',
    'wines/:id': 'wineDetails'
  },

To:

routes: {
    '': 'list',
    '/': 'list',
    'wines/add': 'wineAdd',
    'wines/:id': 'wineDetails'
  },

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