Skip to content

Commit

Permalink
New Build
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Olson committed Aug 20, 2018
1 parent d89eaf0 commit 08ecbb6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 50 deletions.
12 changes: 1 addition & 11 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,13 @@ grunt.initConfig({

uglify: {
"backbone-fiber.min.js": [ "<banner>", "backbone-fiber.js" ]
},

jshint: {
options: {
jshintrc: ".jshintrc"
},
files: {
src: [ "src/backbone-fiber.js" ]
}
}

});

grunt.loadNpmTasks( "grunt-contrib-jshint" );
grunt.loadNpmTasks( "grunt-contrib-uglify" );
grunt.loadNpmTasks( "grunt-contrib-concat" );

grunt.registerTask( "default", [ "jshint", "concat", "uglify" ] );
grunt.registerTask( "default", [ "concat", "uglify" ] );

};
83 changes: 47 additions & 36 deletions backbone-fiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
var Fiber,
_view_defs = {},
_view_inst = {},
_view_loading = {};
_view_loading = {},
_view_creating = {};

// Store the old jQuery.cleanData
var oldClean = $.cleanData;
Expand Down Expand Up @@ -106,9 +107,9 @@
* otherwise, returns nothing.
*/
getPromise: function( view ) {

if ( _view_loading[view] )
return _view_loading[view];
var v;
if ( v = ( _view_creating[view] || _view_loading[view] ) )
return v;
},


Expand Down Expand Up @@ -137,6 +138,8 @@

var dfd = $.Deferred();

dfd.then(function() { delete _view_loading[target]; }, function() { delete _view_loading[target]; } );

if ( _view_defs[target] ) {

dfd.resolve( _view_defs[target] );
Expand All @@ -146,19 +149,16 @@
_view_loading[target] = dfd;
require( [Fiber.viewPath + target, 'text!' + Fiber.viewPath + target + '.html'], function( view, template ) {
if ( view === void 0 || template === void 0 ) {
delete _view_loading[target];
dfd.reject();
} else {
view.prototype.instanceOf = target;
view.prototype.template = _.template( template );
_view_defs[target] = view;

delete _view_loading[target];
dfd.resolve( view );
}
},
function( err ) {
delete _view_loading[target];
dfd.reject( err );
});
}
Expand All @@ -170,32 +170,46 @@
* View factory method. Creates instance, sets up the parent/child
* relationship and renders the view.
*/
function create( view, $el, options ) {
function create( dataView, $el, options ) {

var dfd = $.Deferred();

var inst = new view(_.extend( options, { el: $el[0] } )),
parent, fizzle = false;
dfd.then(function() { delete _view_creating[dataView]; }, function() { delete _view_creating[dataView]; } );

$el.attr( 'data-cid', inst.cid );
_view_inst[inst.cid] = inst;
_view_creating[dataView] = dfd;

parent = $el.parents( '[data-view]' ).first();
load( dataView ).then( function( view ) {

if ( parent.length > 0 ) {
parent = parent.attr( 'data-cid' );
if ( _view_inst[parent] ) {
_view_inst[parent].addChild( inst );
inst.setParent( _view_inst[parent] );
} else {
var inst = new view(_.extend( options, { el: $el[0] } )),
parent, fizzle = false;

$el.attr( 'data-cid', inst.cid );
_view_inst[inst.cid] = inst;

parent = $el.parents( '[data-view]' ).first();

if ( parent.length > 0 ) {
parent = parent.attr( 'data-cid' );
if ( _view_inst[parent] ) {
_view_inst[parent].addChild( inst );
inst.setParent( _view_inst[parent] );
} else {
fizzle = true;
inst.remove();
}
} else if ( $el.closest('body').length === 0 ) {
fizzle = true;
inst.remove();
}
} else if ( $el.closest('body').length === 0 ) {
fizzle = true;
inst.remove();
}

if (!fizzle) { inst.render(); }
return inst;
if (!fizzle) {
inst.render();
dfd.resolve( inst );
} else {
dfd.reject();
}

}, function() { dfd.reject(); } );

}

Expand All @@ -212,11 +226,8 @@

if ( dataView ) {

load( dataView ).done( function( view ) {
create( dataView, $el, options );

create( view, $el, options );

});
}

}
Expand All @@ -226,13 +237,13 @@

Backbone.View.mix([{


/**
* Flag used to track whether the view is still connected to the dom
* Useful for deferred functions to know if it still needs to run
*/
_connected: true,

/**
* load() will attach the compiled template to the
* prototype of the loaded view. No need to do anything
Expand Down Expand Up @@ -334,14 +345,14 @@
* onError [ Function ] ( optional )
*
* Function to call if one of the views fails to load.
*/
waitFor: function( children, onSuccess, onError ) {

var self = this,
sync = _.compact( _.map( children, function( v ) { return Fiber.getPromise( v ); } ) );

$.when.apply( this, sync ).then(
$.when.apply( this, sync ).then(
function() {
onSuccess.apply( self, arguments );
},
Expand Down Expand Up @@ -662,7 +673,7 @@
isMyChild: function( view ) {
return ( view.parent && _.find( this.children, function( cid ) { return view.cid == cid; } ) !== void 0 ) || ( !view.parent && this.isMyElement( view.$el.parent() ) );
},

/**
* quick method for children to determine if it is loaded
*/
Expand Down Expand Up @@ -691,9 +702,9 @@
var $el = (el instanceof $ ? el : $(el));

return ( $el.closest('[data-view]').first().attr('data-cid') == this.cid );

},

/**
* Determine if still connected
*/
Expand Down
2 changes: 1 addition & 1 deletion backbone-fiber.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "backbone-fiber",
"version": "0.1.7",
"version": "0.1.8",
"ignore": [
"bower_components",
"node_modules",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Backbone.Fiber",
"title": "",
"description": "",
"version": "0.1.7",
"version": "0.1.8",
"author": {
"name": "Ben Olson",
"url": "http://bseth99.github.com/"
Expand Down

0 comments on commit 08ecbb6

Please sign in to comment.