Skip to content

Commit

Permalink
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 1f55d9d commit 4bb4089
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 181 deletions.
3 changes: 2 additions & 1 deletion realtime-angular-model/buildr/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"lodash": "~2.4.1",
"q": "~1.0.0",
"eventEmitter": "~4.2.7",
"angular-truncate": "*"
"angular-truncate": "*",
"goangular": "~3.3.4"
}
}
11 changes: 8 additions & 3 deletions realtime-angular-model/buildr/public/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/* global angular */

angular.module('Buildr', [
'truncate'
]);
angular
.module('Buildr', [
'truncate',
'goangular'
])
.config(function($goConnectionProvider) {
$goConnectionProvider.$set('https://goinstant.net/mattcreager/buildr');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* global angular */

var ROSTER_ID = 'dbc93961dff2a934';

(function() { 'use strict';

angular
.module('Buildr')
.controller('RosterCtrl', ['$scope', 'RosterModel', function($scope, Roster) {
$scope.roster = Roster.$find(ROSTER_ID);
$scope.roster.$getUnits();
}]);

})();

Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,31 @@ function RosterModel(futureRosterData) {

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

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

return RosterModel;
}];

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

RosterModel.$find = function(uid) {
var futureRosterData = this.$$resource.find(uid);

if (uid) return new RosterModel(futureRosterData);

return RosterModel.$unwrapCollection(futureRosterData);
};

RosterModel.prototype.$$emitter = _.clone(EventEmitter.prototype);
Expand All @@ -41,7 +44,7 @@ RosterModel.prototype.$getUnits = function() {

return this.$futureRosterData.get('units').then(function(rosterUnits) {
var units = _.reduce(rosterUnits, function(a, rosterUnit) {
var unit = RosterModel.$Unit.$find(rosterUnit.unit_id);
var unit = RosterModel.$Unit.$find(rosterUnit.id);
_.extend(unit, rosterUnit);
a.push(unit);
return a;
Expand All @@ -52,6 +55,7 @@ RosterModel.prototype.$getUnits = function() {
});

return Q.all(_.pluck(units, '$futureUnitData')).then(function() {
self.$$emitter.emit('update');
return self.units;
});
});
Expand Down Expand Up @@ -97,64 +101,29 @@ RosterModel.prototype.$remove = function(unit) {

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

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

RosterModel.prototype.$acceptSuggestion = function(unit) {
unit.isSuggestion = false;

return this.$saveUnits();
};

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

unit.count = 1;
unit.isSuggestion = true;
this.units.push(unit);

return this.$saveUnits();
this.suggestions.$addSuggestion(unit);
};

function FantasyRoster() {
console.log('fantasyRoster kick-off');
}

inheritPrototype(FantasyRoster, RosterModel);
RosterModel.prototype.$acceptSuggestion = function(unit) {
var self = this;

FantasyRoster.prototype.$validate = function() {
console.log(this, 'validation');
return this.suggestions.$removeSuggestion(unit).then(function() {
return self.$add(unit);
});
};

// var createObject = Object.create;

// if (!_.isFunction(createObject)) {
// createObject = function(obj) {
// function F() {}

// F.prototype = obj;
// return new F();
// };
// }

function inheritPrototype(SubClass, SuperClass) {
var superCopy = Object.create(SuperClass.prototype);

superCopy.constructor = SubClass;

SubClass.prototype = superCopy;

for (var i in SuperClass) {
SubClass[i] = SuperClass[i];
}
}

angular.module('Buildr').factory('RosterModel', RosterModel.$factory);
RosterModel.prototype.$declineSuggestion = function(unit) {
return this.suggestions.$removeSuggestion(unit);
};

})();

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* global angular*/

(function() { 'use strict';

angular.module('Buildr').factory('bdSuggestions', function($goKey) {
var suggestions = $goKey('suggestion/');

suggestions.$removeSuggestion = function(unit) {
return this.$key(unit.id).$remove();
};

suggestions.$addSuggestion = function(unit) {
return this.$key(unit.id).$set(unit.$omit());
};

return suggestions;
});

})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ angular
'$scope',
'UnitModel',
function($scope, Unit) {

//$scope.blahUnit = Unit.$find('4826f1ef3d292b6f');
$scope.units = Unit.$find();

//console.log($scope.units, $scope.blahUnit)

// $scope.$watch('blahUnit', function() {
// console.log(arguments)
// })
}]);

})();
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ UnitModel.$factory = ['$timeout', 'bdResource', function($timeout, Resource) {
return UnitModel;
}];

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

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

Expand Down Expand Up @@ -56,6 +58,10 @@ UnitModel.prototype.$unwrap = function() {
});
};

angular.module('Buildr').factory('UnitModel', UnitModel.$factory);
UnitModel.prototype.$omit = function() {
return _.omit(this, function(value, key){
return _.first(key) === '$' || key === 'constructor';
});
};

})();
Loading

0 comments on commit 4bb4089

Please sign in to comment.