Skip to content

Instantly share code, notes, and snippets.

@SET001
Created December 23, 2011 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SET001/1512589 to your computer and use it in GitHub Desktop.
Save SET001/1512589 to your computer and use it in GitHub Desktop.
Editor = Backbone.Router.extend({
immo: {},
lm: {},
nav: [],
pages: [],
routes: {
'editor/basic': 'basic_page',
'editor/basic/:id': 'basic_page',
'editor/details': 'details_page',
'editor/details/:id': 'details_page',
'editor/photos': 'photos_page',
'editor/photos/:id': 'photos_page',
'editor/publish': 'publish_page',
'editor/publish/:id': 'publish_page',
'editor/save/' : 'publish',
'editor/save/:id' : 'publish',},
initialize: function(){
var self = this;
if (!_length(self.immo)){
self.immo = new Immo_Object();
}
},
basic_page: function(id){
var self = this;
self.init(new View_EditorBasic({model: self.immo}));
self.nav.push({caption: '> Next Step', link: 'details'});
self.run(id);
},
details_page: function(id){
var self = this;
self.init(new View_EditorDetails({model: self.immo}));
self.nav.push({caption: '< Prev Step', link: 'basic'});
self.nav.push({caption: '> Next Step', link: 'photos'});
self.run(id);
},
photos_page: function(id){
var self = this;
self.init(new View_EditorPhotos({model: self.immo}));
self.nav.push({caption: '< Prev Step', link: 'details'});
self.nav.push({caption: '> Next Step', link: 'publish'});
self.run(id);
},
publish_page: function(id){
var self = this;
self.init(new View_EditorPublish({model: self.immo}));
self.nav.push({caption: '< Prev Step', link: 'photos'});
self.nav.push({caption: '> Publish', link: 'save'});
self.run(id);
},
publish: function(){
var self = this;
$.get(SITE_PATH + 'immo/save/'+self.immo.get('id'), self.immo.toJSON(), function(data){
Backbone.history.navigate('details/' + $.parseJSON(data).id, true);
});
return false;
},
init: function(page){
var self = this;
self.nav = [];
self.pages = [
{name: 'basic', caption: 'Basic Infos'},
{name: 'details', caption: 'Details'},
{name: 'photos', caption: 'Photos'},
{name: 'publish', caption: 'Publish'},
];
self.lm = new Backbone.LayoutManager({
template: 'editor',
views:{
'.editor': new View_Editor({
collection: self.pages,
views: {
'.editor_form': page,
'.editor_top_nav, .editor_bottom_nav': new View_EditorNavigation({collection : self.nav})
}})
}
});
},
done: function(self){
if (!self) self=this;
self.compose_navigation();
self.lm.render(function(el){
$('#content').html(el);
$(el).find(".facts dl").columnize();
});
},
run: function(id){
var self = this;
if (id && !self.immo.get('id')){
self.immo.set({id: id});
self.immo.fetch({success: function(){
return self.done(self);
}});
}else{
if (self.immo.get('id'))
self.immo.clear();
self.store();
self.done();
}
},
compose_navigation: function(){
var self = this;
_.each(self.nav, function(item){
item.link = 'editor/' + item.link;
if (self.immo.get('id')) item.link += '/' + self.immo.get('id');
});
},
/* store object when switching from page to page */
store: function(){
var self = this;
var form = $('.editor_form form').serializeObject();
self.immo.set(form);
return true;
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment