Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
Simply the models to make it more canonical to Backbone MVC
Browse files Browse the repository at this point in the history
  • Loading branch information
trongtt committed Oct 29, 2013
1 parent e13ac29 commit 986a4dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 59 deletions.
Expand Up @@ -2,18 +2,6 @@

// An abstract model for Application and Container (Zone) in a layout
var LayoutComponent = Backbone.Model.extend({

initialize : function() {

// TODO: We should rely on Backbone.Model.defaults property instead
// TODO: Should we remove these default attributes ?
this.set({
_parent : null,
draggable : false,
droppable : false
});
},

isDroppable : function(dragObj) {
return false;
},
Expand All @@ -25,7 +13,7 @@

// Return the parent object
getParent : function() {
return this.get('_parent');
return this._parent;
},

// Return the root object
Expand Down Expand Up @@ -98,43 +86,6 @@
_this.setContent(result.content);
}
});
},

// TODO: Why do we need to overwrite the clone method from Backbone.Model ?
// It should be tested
clone: function() {
return new Application({
id: this.getId(),
name : this.getName(),
applicationName : this.getApplicationName(),
title : this.getTitle(),
logo : this.getLogo(),
content: this.getContent()
});
},

// Return the JSON object that contains metadata information
// TODO: Should we rely on the default #toJSON method from Backbone.Model ?
toJSON : function() {
return {
id : this.getId(),
type : "application",
name : this.getName(),
applicationName: this.getApplicationName()
};
},

// Return a JSON object for rendering phase
// TODO: should it be merged with #toJSON() method ?
toJSONForRenderer : function() {
return {
id : this.getId(),
type : "application",
name : this.getName(),
title : this.getTitle(),
content : this.getContent(),
logo : this.getLogo()
};
}
});

Expand Down Expand Up @@ -192,7 +143,7 @@
silent : options.silent
});
}
child.set('_parent', this);
child._parent = this;
// collection in backbone ignore move action in same container
// need to remove then re-add
this._children.remove(child, {
Expand All @@ -216,7 +167,7 @@
this._children.remove(child, {
silent : options.silent
});
child.set('_parent', null);
child._parent = null;
}
return this;
},
Expand Down Expand Up @@ -267,7 +218,7 @@
});

//
var PageContainer = AbstractContainer.extend({
var PageLayout = AbstractContainer.extend({

isDroppable : function(dragObj) {
// Check for supported types
Expand All @@ -287,7 +238,7 @@
return this.get('layout_id');
},

// newContainer: is the new PageContainer object
// newContainer: is the new PageLayout object
switchLayout : function(newContainer) {
var conts = this.getChildren();
conts.sort(function(m1, m2) {
Expand Down Expand Up @@ -369,7 +320,7 @@
'LayoutComponent' : LayoutComponent,
'Application' : Application,
'Container' : Container,
'PageContainer' : PageContainer,
'PageLayout' : PageLayout,
'ComposerContainer' : ComposerContainer
};
if (typeof window.require === "function" && window.require.amd) {
Expand Down
Expand Up @@ -15,7 +15,7 @@
onAddChild : function(child) {
var $container = $('#application-list');

var html = _.template($("#portlet-template").html(), child.toJSONForRenderer());
var html = _.template($("#portlet-template").html(), child.toJSON());
var $html = $(html);
$container.append($html);

Expand All @@ -38,7 +38,7 @@
},
render: function() {
this.template = _.template($("#application-template").html());
this.$el.html(this.template(this.model.toJSONForRenderer()));
this.$el.html(this.template(this.model.toJSON()));
this.$el.attr("id", this.model.getId());
return this;
},
Expand Down Expand Up @@ -234,8 +234,8 @@
// Build model from DOM
buildModel : function() {

// TODO: Consider to initialize PageContainer model's url properly following Backbone standard
var _model = new PageContainer({id : 'layoutId'}, {url : this.editUrl});
// TODO: Consider to initialize PageLayout model's url properly following Backbone standard
var _model = new PageLayout({id : 'layoutId'}, {url : this.editUrl});
this.$el.find('.sortable').each(function() {
var cont = new Container({id : this.id});
$(this).children('.portlet').each(function() {
Expand Down

0 comments on commit 986a4dd

Please sign in to comment.