move Ghost.View out of init, refactor

This commit is contained in:
Ricardo Tomasi 2013-06-07 23:16:28 -03:00
parent aa659d29b6
commit dc714611a9
4 changed files with 58 additions and 56 deletions

View File

@ -21,46 +21,15 @@
router: null
};
Ghost.View = Backbone.View.extend({
// Adds a subview to the current view, which will
// ensure its removal when this view is removed,
// or when view.removeSubviews is called
addSubview: function (view) {
if (!(view instanceof Backbone.View)) {
throw new Error("Subview must be a Backbone.View");
}
this.subviews = this.subviews || [];
this.subviews.push(view);
return view;
},
// Removes any subviews associated with this view
// by `addSubview`, which will in-turn remove any
// children of those views, and so on.
removeSubviews: function () {
var i, l, children = this.subviews;
if (!children) {
return this;
}
for (i = 0, l = children.length; i < l; i += 1) {
children[i].remove();
}
this.subviews = [];
return this;
},
// Extends the view's remove, by calling `removeSubviews`
// if any subviews exist.
remove: function () {
if (this.subviews) {
this.removeSubviews();
}
return Backbone.View.prototype.remove.apply(this, arguments);
}
});
Ghost.init = function () {
Ghost.router = new Ghost.Router();
Backbone.history.start({
pushState: true,
hashChange: false,
root: '/ghost'
});
};
window.Ghost = Ghost;
}());
}());

View File

@ -1,15 +0,0 @@
/*global window, document, Ghost, Backbone, $, _ */
(function () {
"use strict";
Ghost.router = new Ghost.Router();
$(function () {
Backbone.history.start({pushState: true, hashChange: false, root: '/ghost'});
});
}());

View File

@ -0,0 +1,45 @@
/*global window, document, Ghost, Backbone, $, _ */
(function () {
"use strict";
Ghost.View = Backbone.View.extend({
// Adds a subview to the current view, which will
// ensure its removal when this view is removed,
// or when view.removeSubviews is called
addSubview: function (view) {
if (!(view instanceof Backbone.View)) {
throw new Error("Subview must be a Backbone.View");
}
this.subviews = this.subviews || [];
this.subviews.push(view);
return view;
},
// Removes any subviews associated with this view
// by `addSubview`, which will in-turn remove any
// children of those views, and so on.
removeSubviews: function () {
var i, l, children = this.subviews;
if (!children) {
return this;
}
for (i = 0, l = children.length; i < l; i += 1) {
children[i].remove();
}
this.subviews = [];
return this;
},
// Extends the view's remove, by calling `removeSubviews`
// if any subviews exist.
remove: function () {
if (this.subviews) {
this.removeSubviews();
}
return Backbone.View.prototype.remove.apply(this, arguments);
}
});
}());

View File

@ -56,10 +56,13 @@
<!-- // require '/core/admin/assets/js/models/*' -->
<script src="/core/admin/assets/js/models/post.js"></script>
<!-- // require '/core/admin/assets/js/views/*' -->
<script src="/core/admin/assets/js/views/base.js"></script>
<script src="/core/admin/assets/js/views/blog.js"></script>
<script src="/core/admin/assets/js/views/editor.js"></script>
<script src="/core/admin/assets/js/router.js"></script>
<script src="/core/admin/assets/js/starter.js"></script>
{{{block "bodyScripts"}}}
<script>
Ghost.init();
</script>
</body>
</html>