Skip to content
This repository has been archived by the owner on Jun 26, 2022. It is now read-only.

Commit

Permalink
Switch to Exoskeleton from Backbone. Drop jQuery & underscore.
Browse files Browse the repository at this point in the history
* Dropped jQuery and underscore dependencies.
* Added Davy https://github.com/lvivski/davy for promises.
* Exoskeleton is a no-deps backbone fork http://exosjs.com

See bower.json for the main change you will need to do.
  • Loading branch information
paulmillr committed Oct 29, 2013
1 parent 5c40ffb commit 514ba86
Show file tree
Hide file tree
Showing 14 changed files with 61 additions and 61 deletions.
6 changes: 3 additions & 3 deletions app/controllers/auth-controller.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Controller = require 'controllers/base/controller'

This comment has been minimized.

Copy link
@paulmillr

paulmillr Nov 3, 2013

Author Owner

Check out commit 74299b5 for additional work total removal of jQuery.

utils = require 'lib/utils'

module.exports = class AuthController extends Controller
callback: (params) ->
_.extend params, _.object window.location.search
.slice(1).split('&')
.map((string) -> string.split('='))
parsed = utils.queryParams.parse window.location.search
Backbone.utils.extend params, parsed
console.log 'AuthController#callback', params
@publishEvent 'auth:setToken', params.accessToken
@redirectToRoute 'users#show', [params.login]
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/users-controller.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ module.exports = class UsersController extends Controller
# “Sync repos” button.
@repoSync = new Collection null, model: Repo
@repoSync.url = @model.url('/sync_repos/')
@repoSync.fetch = (options) => $.post @repoSync.url
@repoSync.fetch = (options) =>
Backbone.utils.ajax
url: @repoSync.url,
method: 'POST'
@repoSyncView = new UserRepoSyncView
collection: @repoSync,
region: 'sync-repos'
Expand Down
7 changes: 6 additions & 1 deletion app/initialize.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
Application = require 'application'
routes = require 'routes'

Backbone.Deferred = -> new window.Davy
Backbone.resolveDeferred = (deferred, isResolved, args) ->
deferred[if isResolved then 'fulfill' else 'reject'].apply deferred, args

# Initialize the application on DOM ready event.
$ ->
document.addEventListener 'DOMContentLoaded', ->
new Application {
title: 'Ost.io',
controllerSuffix: '-controller',
routes
}
, false
18 changes: 10 additions & 8 deletions app/lib/services/ostio.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = class Ostio extends ServiceProvider
constructor: ->
super
@accessToken = localStorage.getItem 'accessToken'
authCallback = _.bind(@loginHandler, this, @loginHandler)
authCallback = @loginHandler.bind this, @loginHandler
@subscribeEvent 'auth:setToken', @setToken
@subscribeEvent 'auth:callback:ostio', authCallback

Expand All @@ -21,7 +21,7 @@ module.exports = class Ostio extends ServiceProvider
@accessToken = token

load: ->
@resolve()
@fulfill()
this

isLoaded: ->
Expand All @@ -31,11 +31,13 @@ module.exports = class Ostio extends ServiceProvider
console.log 'ajax', url, @accessToken, this
url = @baseUrl + url
url += "?access_token=#{@accessToken}" if @accessToken
$.ajax {url, data, type, dataType: 'json'}
Backbone.utils.ajax {
url, data, type, headers: {Accept: 'application/json'}
}

# Trigger login popup
triggerLogin: (loginContext) ->
callback = _.bind(@loginHandler, this, @loginHandler)
callback = @loginHandler.bind this, @loginHandler
window.location = URL

# Callback for the login popup
Expand All @@ -47,7 +49,7 @@ module.exports = class Ostio extends ServiceProvider
@publishEvent 'loginSuccessful', {provider: this, loginContext}

# Publish the session
@getUserData().done(@processUserData)
@getUserData().then @processUserData
else
@publishEvent 'loginFail', provider: this, loginContext: loginContext

Expand All @@ -58,14 +60,14 @@ module.exports = class Ostio extends ServiceProvider
@publishEvent 'userData', response

getLoginStatus: (callback = @loginStatusHandler, force = false) ->
@getUserData().always(callback)
@getUserData().then(callback, callback)

loginStatusHandler: (response, status) =>
if not response or status is 'error'
if not response or response.status is 401
@publishEvent 'logout'
else
parsed = User::parse.call(null, response)
@publishEvent 'serviceProviderSession', _.extend parsed,
@publishEvent 'serviceProviderSession', Backbone.utils.extend parsed,
provider: this
userId: response.id
accessToken: @accessToken
6 changes: 3 additions & 3 deletions app/lib/services/service-provider.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ utils = require 'lib/utils'
module.exports = class ServiceProvider

# Mixin an Event Broker
_.extend @prototype, Chaplin.EventBroker
Backbone.utils.extend @prototype, Chaplin.EventBroker

loading: false

constructor: ->
###console.debug 'ServiceProvider#constructor'###

# Mixin a Deferred
_.extend this, $.Deferred()
Backbone.utils.extend this, new window.Davy()

utils.deferMethods
deferred: this
Expand Down Expand Up @@ -55,7 +55,7 @@ module.exports = class ServiceProvider
# Trigger login popup
triggerLogin: (loginContext) ->
callback = _.bind(@loginHandler, this, loginContext)
callback = @loginHandler.bind this, @loginHandler
ServiceProviderLibrary.login callback
# Callback for the login popup
Expand Down
12 changes: 0 additions & 12 deletions app/lib/support.coffee

This file was deleted.

6 changes: 3 additions & 3 deletions app/lib/utils.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Delegate to Chaplin’s utils module
utils = Chaplin.utils.beget Chaplin.utils

_.extend utils,
Backbone.utils.extend utils,
# Functional helpers for handling asynchronous dependancies and I/O
# -----------------------------------------------------------------

Expand Down Expand Up @@ -111,12 +111,12 @@ found on host #{host}"
->
# Save the original arguments
args = arguments
if deferred.state() is 'resolved'
if deferred.isFulfilled
# Deferred already resolved, call func immediately
func.apply context, args
else
# Register a done handler
deferred.done ->
deferred.then ->
func.apply context, args
# Invoke the onDeferral callback
if typeof onDeferral is 'function'
Expand Down
10 changes: 5 additions & 5 deletions app/models/post.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ module.exports = class Post extends Model

parse: (response) ->
if response?
repoUser = new User _.extend response.topic.repo.user, {}
repo = new Repo _.extend response.topic.repo, {user: repoUser}
topic = new Topic _.extend response.topic, {repo}
user = new User _.extend response.user, {}
_.extend response, {topic}
repoUser = new User Backbone.utils.extend response.topic.repo.user, {}
repo = new Repo Backbone.utils.extend response.topic.repo, {user: repoUser}
topic = new Topic Backbone.utils.extend response.topic, {repo}
user = new User Backbone.utils.extend response.user, {}
Backbone.utils.extend response, {topic}
4 changes: 2 additions & 2 deletions app/models/repo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ module.exports = class Repo extends Model
"/users/#{@get('user').get('login')}/repos/"

parse: (response) ->
user = new User _.extend response.user, {}
_.extend response, {user}
user = new User Backbone.utils.extend response.user, {}
Backbone.utils.extend response, {user}
6 changes: 3 additions & 3 deletions app/models/topic.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ module.exports = class Topic extends Model

parse: (response) ->
Post = require 'models/post'
user = new User _.extend response.repo.user, {}
repo = new Repo _.extend response.repo, {user}
_.extend response, {repo}
user = new User Backbone.utils.extend response.repo.user, {}
repo = new Repo Backbone.utils.extend response.repo, {user}
Backbone.utils.extend response, {repo}
4 changes: 2 additions & 2 deletions app/models/user.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ module.exports = class User extends Model
options = {model: User}
if response.organizations?
organizations = new Collection response.organizations, options
_.extend response, {organizations}
Backbone.utils.extend response, {organizations}
if response.owners?
owners = new Collection response.owners, options
_.extend response, {owners}
Backbone.utils.extend response, {owners}
response

# Ideally, there should be a special model field for this.
Expand Down
7 changes: 4 additions & 3 deletions app/views/base/form-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ module.exports = class FormView extends View

save: (event) =>
spinner = new SpinnerView container: @$('.submit-form')
dispose = (response) => spinner.dispose()
@model.save()
.done (response) =>
.then (response) =>
@publishSave response
@dismiss()
.always (response) =>
spinner.dispose()
dispose()
, dispose

submit: (event) =>
event.preventDefault()
Expand Down
11 changes: 6 additions & 5 deletions app/views/topic/new-topic-form-view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,18 @@ module.exports = class NewTopicFormView extends FormView
spinner = new SpinnerView container: @$('.submit-form')
end = -> spinner.dispose()
@model.save()
.done (response) =>
.then (response) =>
@post.save()
.done (postResponse) =>
.then (postResponse) =>
@$('.new-topic-form-toggle-fields-button').click()
@publishSave response
@trigger 'dispose'
@dispose()
.fail (error) =>
end()
, (error) =>
@model.destroy()
.always(end)
.fail(end)
end()
, end

dispose: ->
return if @disposed
Expand Down
20 changes: 10 additions & 10 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
"components"
],
"dependencies": {
"chaplin": "~0.10.0",
"chaplin": "~0.11.3",
"console-polyfill": "~0.1.0",
"lodash": "~1.3.1",
"moment": "~2.0.0",
"jquery": "~2.0.2",
"normalize-css": "~2.1.2"
"normalize-css": "~2.1.2",
"exoskeleton": "~0.4.0",
"davy": "~0.0.5"
},
"overrides": {

This comment has been minimized.

Copy link
@balupton

balupton Oct 31, 2013

Never seen this before? Which package manager describes this? What does it do? super curious

This comment has been minimized.

Copy link
@paulmillr

paulmillr Oct 31, 2013

Author Owner

this is my read-components npm plugin.

It reads root bower.json, then reads bower.json of all dependency packages and applies overrides from root json.

This allows to construct a list of files in right order. If backbone depends on jquery and marionette depends on backbone, then the order will be jquery.js -> backbone.js -> marionette.js. The plugin returns list of all files from all deps and their sorting index.

This comment has been minimized.

Copy link
@paulmillr

paulmillr Oct 31, 2013

Author Owner

Also I proposed this in bower core. bower/bower#585

"backbone": {
"chaplin": {
"dependencies": {
"lodash": "*",
"jquery": "*"
},
"main": "backbone.js"
"exoskeleton": "*"
}
},
"moment": {"main": "moment.js"}
"moment": {
"main": "moment.js"
}
}
}

0 comments on commit 514ba86

Please sign in to comment.