Skip to content

Commit

Permalink
moar last minute example updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Creager committed Apr 7, 2014
1 parent 4bb4089 commit 8e1c696
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Resource.factory = function($http) {
};
};

angular.module('Buildr').factory('bdResource', Resource.factory);

Resource.prototype.find = function(uid) {
var deferred = Q.defer();

Expand All @@ -40,6 +42,4 @@ Resource.prototype.set = function(uid, newValue) {
return deferred.promise;
};

angular.module('Buildr').factory('bdResource', Resource.factory);

})();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var ROSTER_ID = 'dbc93961dff2a934';

angular
.module('Buildr')
.controller('RosterCtrl', ['$scope', 'RosterModel', function($scope, Roster) {
.controller('RosterCtrl', ['$scope', 'bdRoster', function($scope, Roster) {
$scope.roster = Roster.$find(ROSTER_ID);
$scope.roster.$getUnits();
}]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,45 @@

(function() { 'use strict';

function RosterModel(futureRosterData) {
if (!futureRosterData.inspect) {
_.extend(this, futureRosterData);
return;
}

this.$futureRosterData = futureRosterData;
function Roster(futureRosterData) {
this.suggestions = Roster.$suggestions.$sync();
this.$unwrap(futureRosterData);
this.suggestions = RosterModel.$suggestions.$sync();
}

RosterModel.$factory = [
Roster.$factory = [
'$timeout',
'bdResource',
'bdSuggestions',
'UnitModel',
'bdUnit',
function($timeout, Resource, suggestions, Unit) {
_.extend(RosterModel, {
_.extend(Roster, {
$$resource: new Resource('/rosters'),
$timeout: $timeout,
$suggestions: suggestions,
$Unit: Unit
});

return RosterModel;
return Roster;
}];

angular.module('Buildr').factory('RosterModel', RosterModel.$factory);

RosterModel.$find = function(uid) {
var futureRosterData = this.$$resource.find(uid);
angular.module('Buildr').factory('bdRoster', Roster.$factory);

if (uid) return new RosterModel(futureRosterData);
Roster.$find = function(uid) {
return new Roster(this.$$resource.find(uid));
};

RosterModel.prototype.$$emitter = _.clone(EventEmitter.prototype);

RosterModel.prototype.$getUnits = function() {
Roster.prototype.$getUnits = function() {
var self = this;

return this.$futureRosterData.get('units').then(function(rosterUnits) {
var units = _.reduce(rosterUnits, function(a, rosterUnit) {
var unit = RosterModel.$Unit.$find(rosterUnit.id);
var unit = Roster.$Unit.$find(rosterUnit.id);
_.extend(unit, rosterUnit);
a.push(unit);
return a;
}, []);

RosterModel.$timeout(function() {
Roster.$timeout(function() {
self.units = units;
});

Expand All @@ -61,28 +51,29 @@ RosterModel.prototype.$getUnits = function() {
});
};

RosterModel.prototype.$unwrap = function() {
Roster.prototype.$unwrap = function(futureRosterData) {
var self = this;

this.$futureRosterData = futureRosterData;
this.$futureRosterData.then(function(data) {
RosterModel.$timeout(function() { _.extend(self, data); });
Roster.$timeout(function() { _.extend(self, data); });
});
};

RosterModel.prototype.$inc = function(unit) {
Roster.prototype.$inc = function(unit) {
unit = _.find(this.units, { id: unit.id });
unit.count++;

return this.$saveUnits();
};

RosterModel.prototype.$dec = function(unit) {
Roster.prototype.$dec = function(unit) {
unit.count--;

return this.$saveUnits();
};

RosterModel.prototype.$add = function(unit) {
Roster.prototype.$add = function(unit) {
if (_.contains(_.pluck(this.units, 'id'), unit.id)) return this.$inc(unit);

unit.count = 1;
Expand All @@ -91,38 +82,38 @@ RosterModel.prototype.$add = function(unit) {
return this.$saveUnits();
};

RosterModel.prototype.$remove = function(unit) {
Roster.prototype.$remove = function(unit) {
if (unit.count > 1) return this.$dec(unit);

this.units.splice(this.units.indexOf(unit), 1);

return this.$saveUnits();
};

RosterModel.prototype.$saveUnits = function() {
Roster.prototype.$saveUnits = function() {
var units = _.map(this.units, function(unit) {
return _.pick(unit, ['id', 'count']);
});

return RosterModel.$$resource.set(this.id, { units: units });
return Roster.$$resource.set(this.id, { units: units });
};

RosterModel.prototype.$suggest = function(unit) {
Roster.prototype.$suggest = function(unit) {
if (_.contains(_.pluck(this.units, 'id'), unit.id)) return;

unit.count = 1;
this.suggestions.$addSuggestion(unit);
};

RosterModel.prototype.$acceptSuggestion = function(unit) {
Roster.prototype.$acceptSuggestion = function(unit) {
var self = this;

return this.suggestions.$removeSuggestion(unit).then(function() {
return self.$add(unit);
});
};

RosterModel.prototype.$declineSuggestion = function(unit) {
Roster.prototype.$declineSuggestion = function(unit) {
return this.suggestions.$removeSuggestion(unit);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

angular
.module('Buildr')
.controller('UnitCtrl', [
'$scope',
'UnitModel',
function($scope, Unit) {
.controller('UnitCtrl', ['$scope', 'bdUnit', function($scope, Unit) {
$scope.units = Unit.$find();
}]);
}]);

})();
30 changes: 15 additions & 15 deletions realtime-angular-model/buildr/public/components/unit/unit-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

(function() { 'use strict';

function UnitModel(futureUnitData) {
function Unit(futureUnitData) {
if (!futureUnitData.inspect) {
_.extend(this, futureUnitData);
return;
Expand All @@ -12,34 +12,34 @@ function UnitModel(futureUnitData) {
this.$unwrap(futureUnitData);
}

UnitModel.$factory = ['$timeout', 'bdResource', function($timeout, Resource) {
_.extend(UnitModel, {
Unit.$factory = ['$timeout', 'bdResource', function($timeout, Resource) {
_.extend(Unit, {
$$resource: new Resource('/units'),
$timeout: $timeout
});

return UnitModel;
return Unit;
}];

angular.module('Buildr').factory('UnitModel', UnitModel.$factory);
angular.module('Buildr').factory('bdUnit', Unit.$factory);

UnitModel.$find = function(uid) {
Unit.$find = function(uid) {
var futureUnitData = this.$$resource.find(uid);

if (uid) return new UnitModel(futureUnitData);
if (uid) return new Unit(futureUnitData);

return UnitModel.$unwrapCollection(futureUnitData);
return Unit.$unwrapCollection(futureUnitData);
};

UnitModel.$unwrapCollection = function(futureUnitData) {
Unit.$unwrapCollection = function(futureUnitData) {
var collection = {};

collection.$futureUnitData = futureUnitData;

futureUnitData.then(function(units) {
UnitModel.$timeout(function() {
Unit.$timeout(function() {
_.reduce(units, function(c, unit) {
c[unit.id] = new UnitModel(unit);
c[unit.id] = new Unit(unit);
return c;
}, collection);
});
Expand All @@ -48,17 +48,17 @@ UnitModel.$unwrapCollection = function(futureUnitData) {
return collection;
};

UnitModel.prototype.$$emitter = _.clone(EventEmitter.prototype);
Unit.prototype.$$emitter = _.clone(EventEmitter.prototype);

UnitModel.prototype.$unwrap = function() {
Unit.prototype.$unwrap = function() {
var self = this;

this.$futureUnitData.then(function(data) {
UnitModel.$timeout(function() { _.extend(self, data); });
Unit.$timeout(function() { _.extend(self, data); });
});
};

UnitModel.prototype.$omit = function() {
Unit.prototype.$omit = function() {
return _.omit(this, function(value, key){
return _.first(key) === '$' || key === 'constructor';
});
Expand Down
12 changes: 5 additions & 7 deletions realtime-angular-model/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,13 @@ <h5>Is about <strong>presentation</strong> and <strong>interaction</strong></h5>

<section data-transition="zoom">
<h5>A <em>Controller</em> with a <span class="blue">solid foundation</span></h5>
<pre><code>angular.module('Buildr').controller('UnitCtrl', [
'$scope',
'unitFactory',
function($scope, unitFactory) {
$scope.units = unitFactory();
$scope.units.$initialize();
<pre><code>angular
.module('Buildr')
..controller('UnitCtrl', ['$scope', 'bdUnit', function($scope, Unit) {
$scope.units = Unit.$find();

$scope.editUnit = function(unit) {
$scope.editUnitModal.open(unit);
$scope.editModal.open(unit); // should accept a promise!
};
}]);</code></pre>
<h5>Communicates <strong>behaviour</strong></h5>
Expand Down

0 comments on commit 8e1c696

Please sign in to comment.