Skip to content

Instantly share code, notes, and snippets.

@brian-mann
Created April 27, 2013 05:41
Show Gist options
  • Select an option

  • Save brian-mann/5472020 to your computer and use it in GitHub Desktop.

Select an option

Save brian-mann/5472020 to your computer and use it in GitHub Desktop.
@PlanetExpress.module "Entities", (Entities, App, Backbone, Marionette, $, _) ->
App.commands.setHandler "when:fetched", (entities, callback) ->
xhrs = _.chain([entities]).flatten().pluck("_fetch").value()
# if _.isArray(entities)
# xhrs.push(entity._fetch) for entity in entities
# else
# xhrs.push(entities._fetch)
$.when(xhrs...).done ->
callback()
@mikefowler

Copy link
Copy Markdown

Brian… been implementing your loading view the past few days, the one from Backbone Rails. Regarding this method above, in particular… where is the "_fetch" key on the entities coming from? From what I can tell I don't see Backbone ever add that property to a model or collection. Am I missing something?

@StraDIVar

Copy link
Copy Markdown

mikefowler, _fetch is "private" property of Backbone Model, therefore it isn't described in API documentation

@n-at-han-k

Copy link
Copy Markdown

When looking at the annotated source _fetch isn't located anywhere on the page.

It turns out that Brian has overridden the Backbone.sync method to add the _fetch property.

https://github.com/brian-mann/sc02-loading-views/blob/master/sc02-loading-views-after/app/assets/javascripts/backbone/config/backbone/sync.js.coffee

do (Backbone) ->
    _sync = Backbone.sync

    Backbone.sync = (method, entity, options = {}) ->

        sync = _sync(method, entity, options)
        if !entity._fetch and method is "read"
            entity._fetch = sync
        sync

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment