-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backbone begins #90
Closed
+3,016
−75
Closed
Backbone begins #90
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*globals window, Backbone */ | ||
(function ($) { | ||
"use strict"; | ||
|
||
var Ghost = { | ||
Layout : {}, | ||
View : {}, | ||
Collection : {}, | ||
Model : {}, | ||
|
||
settings: { | ||
baseUrl: '/api/v0.1' | ||
}, | ||
|
||
currentView: null | ||
}; | ||
|
||
window.Ghost = Ghost; | ||
|
||
}()); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/*global window, document, Ghost, $, Backbone, _ */ | ||
(function () { | ||
"use strict"; | ||
|
||
Ghost.Model.Post = Backbone.Model.extend({ | ||
urlRoot: '/api/v0.1/posts/', | ||
defaults: { | ||
status: 'draft' | ||
} | ||
}); | ||
|
||
Ghost.Collection.Posts = Backbone.Collection.extend({ | ||
url: Ghost.settings.baseURL + '/posts', | ||
model: Ghost.Model.Post | ||
}); | ||
|
||
}()); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/*global window, document, Ghost, Backbone, $, _ */ | ||
(function () { | ||
"use strict"; | ||
|
||
// Base view | ||
// ---------- | ||
Ghost.Layout.Blog = Backbone.Layout.extend({ | ||
initialize: function (options) { | ||
this.addViews({ | ||
list : new Ghost.View.ContentList({ el: '.content-list' }), | ||
preview : new Ghost.View.ContentPreview({ el: '.content-preview' }) | ||
}); | ||
|
||
// TODO: render templates on the client | ||
// this.render() | ||
} | ||
}); | ||
|
||
// Add shadow during scrolling | ||
var scrollShadow = function (target, e) { | ||
if ($(e.currentTarget).scrollTop() > 10) { | ||
$(target).addClass('scrolling'); | ||
} else { | ||
$(target).removeClass('scrolling'); | ||
} | ||
}; | ||
|
||
|
||
// Content list (sidebar) | ||
// ----------------------- | ||
Ghost.View.ContentList = Backbone.View.extend({ | ||
initialize: function (options) { | ||
this.$('.content-list-content').on('scroll', _.bind(scrollShadow, null, '.content-list')); | ||
// Select first item | ||
_.defer(function (el) { | ||
el.find('.content-list-content li:first').trigger('click'); | ||
}, this.$el); | ||
}, | ||
|
||
events: { | ||
'click .content-list-content' : 'scrollHandler', | ||
'click .content-list-content li' : 'showPreview' | ||
}, | ||
|
||
showPreview: function (e) { | ||
var item = $(e.currentTarget); | ||
this.$('.content-list-content li').removeClass('active'); | ||
item.addClass('active'); | ||
Backbone.trigger("blog:showPreview", item.data('id')); | ||
} | ||
}); | ||
|
||
// Content preview | ||
// ---------------- | ||
Ghost.View.ContentPreview = Backbone.View.extend({ | ||
initialize: function (options) { | ||
this.listenTo(Backbone, "blog:showPreview", this.showPost); | ||
this.$('.content-preview-content').on('scroll', _.bind(scrollShadow, null, '.content-preview')); | ||
}, | ||
|
||
events: { | ||
'click .post-controls .delete' : 'deletePost', | ||
'click .post-controls .post-edit' : 'editPost' | ||
}, | ||
|
||
deletePost: function (e) { | ||
e.preventDefault(); | ||
this.model.destroy({ | ||
success: function (model) { | ||
// here the ContentList would pick up the change in the Posts collection automatically | ||
// after client-side rendering is implemented | ||
var item = $('.content-list-content li[data-id=' + model.get('id') + ']'); | ||
item.next().add(item.prev()).eq(0).trigger('click'); | ||
item.remove(); | ||
}, | ||
error: function () { | ||
// TODO: decent error handling | ||
console.error('Error'); | ||
} | ||
}); | ||
}, | ||
|
||
editPost: function (e) { | ||
e.preventDefault(); | ||
// for now this will disable "open in new tab", but when we have a Router implemented | ||
// it can go back to being a normal link to '#/ghost/editor/X' | ||
window.location = '/ghost/editor/' + this.model.get('id'); | ||
}, | ||
|
||
showPost: function (id) { | ||
this.model = new Ghost.Model.Post({ id: id }); | ||
this.model.once('change', this.render, this); | ||
this.model.fetch(); | ||
}, | ||
|
||
render: function () { | ||
this.$('.wrapper').html(this.model.get('content_html')); | ||
} | ||
}); | ||
|
||
// Initialize views. | ||
// TODO: move to a `Backbone.Router` | ||
Ghost.currentView = new Ghost.Layout.Blog({ el: '#main' }); | ||
|
||
}()); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Layout manager | ||
// -------------- | ||
/* | ||
* .addChild('sidebar', App.View.Sidebar) | ||
* .childViews.sidebar.$('blah') | ||
*/ | ||
|
||
Backbone.Layout = Backbone.View.extend({ | ||
// default to loading state, reverted on render() | ||
loading: true, | ||
|
||
addViews: function (views) { | ||
if (!this.views) this.views = {} | ||
|
||
_.each(views, function(view, name){ | ||
if (typeof view.model === 'undefined'){ | ||
view.model = this.model | ||
} | ||
this.views[name] = view | ||
}, this) | ||
return this | ||
}, | ||
|
||
renderViews: function (data) { | ||
_.invoke(this.views, 'render', data) | ||
this.trigger('render') | ||
return this | ||
}, | ||
|
||
appendViews: function (target) { | ||
_.each(this.views, function(view){ | ||
this.$el.append(view.el) | ||
}, this) | ||
this.trigger('append') | ||
return this | ||
}, | ||
|
||
destroyViews: function () { | ||
_.each(this.views, function(view){ | ||
view.model = null | ||
view.remove() | ||
}) | ||
return this | ||
}, | ||
|
||
render: function () { | ||
this.loading = false | ||
this.renderViews() | ||
return this | ||
}, | ||
|
||
remove: function () { | ||
this.destroyViews() | ||
Backbone.View.prototype.remove.call(this) | ||
} | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
Sorry, something went wrong.