Skip to content

Commit

Permalink
Refactor normalizeComputedProps into function
Browse files Browse the repository at this point in the history
It's stateless and does not have to be a part of the class object.
  • Loading branch information
myme committed Mar 31, 2013
1 parent e571d32 commit 212268f
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions src/backbonejs-computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ this.Backbone.Model = (function ( Model, _ ) {
'use strict';


function normalizeComputedProps( computedProps ) {
var propSpec, action, prop, depends;
var normalizedProps = {};

if ( ! computedProps ) {
return {};
}

for ( prop in computedProps ) {
if ( computedProps.hasOwnProperty( prop ) ) {
propSpec = computedProps[ prop ];

if ( propSpec instanceof Function ) {
action = propSpec;
depends = [];
} else {
action = propSpec.action;
depends = propSpec.depends || [];
}

normalizedProps[ prop ] = {
action: action,
depends: depends
};
}
}

return normalizedProps;
}


return Model.extend({

_computedProps: {},
Expand Down Expand Up @@ -121,7 +152,7 @@ this.Backbone.Model = (function ( Model, _ ) {
properties = properties || {};

// Setup computed properties
var computedProps = this.normalizeComputedProps( properties.properties );
var computedProps = normalizeComputedProps( properties.properties );
delete properties.properties;

// Extend our class from Boostrap.Model
Expand All @@ -132,37 +163,7 @@ this.Backbone.Model = (function ( Model, _ ) {
});

return Class;
}),

normalizeComputedProps: function ( computedProps ) {
var propSpec, action, prop, depends;
var normalizedProps = {};

if ( ! computedProps ) {
return {};
}

for ( prop in computedProps ) {
if ( computedProps.hasOwnProperty( prop ) ) {
propSpec = computedProps[ prop ];

if ( propSpec instanceof Function ) {
action = propSpec;
depends = [];
} else {
action = propSpec.action;
depends = propSpec.depends || [];
}

normalizedProps[ prop ] = {
action: action,
depends: depends
};
}
}

return normalizedProps;
}
})

});

Expand Down

0 comments on commit 212268f

Please sign in to comment.