AmpersandJS / ampersand-view

A smart base view for Backbone apps, to make it easy to bind collections and properties to the DOM.
http://ampersandjs.com
MIT License
92 stars 39 forks source link

waitfor array #202

Open RickButler opened 6 years ago

RickButler commented 6 years ago

Add the ability to use an array of string paths for waitFor.

some random things included in this: remove unused require statements normalize spacing around function declarations

RickButler commented 6 years ago

I'm constantly creating derived properties to test for successful fetching of data before rendering a subview.

for example I will have a view with two models (or more).

children: {
    exampleModelOne: MyModel,
    exampleModelTwo: MyModel
}

I have properties that I set when they have been fetched . I then have a derived listening to these props.

derived: {
    viewReady: {
        deps: ['exampleModelOne.fetched', 'exampleModelTwo.fetched'],
        fn: function () { return this.exampleModelOne.fetched && this.exampleModelTwo.fetched; }
}

and then:

subview: {
    myStuff: {
        selector: '[data-hook=collection-container]',
        waitFor: 'viewReady',
        prepareView: function (el) { //do stuff }
    }
}

This would allow skipping the derived and instead just using multiple waitFor:

subview: {
    myStuff: {
        selector: '[data-hook=collection-container]',
        waitFor: ['exampleModelOne.fetched', exampleModelTwo.fetched],
        prepareView: function (el) { //do stuff }
    }
}

The overhead for this would be no worse than creating the derived, the average case may event be better because once 'action' gets called it removes the event listener, instead of the derived catching changes for its deps and any other overall "change" events for the entire lifecycle of the view.

Looking for someone else to sign off on this since I wrote it, @dhritzkiv if you have some time maybe?