Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function() {
'use strict';
angular.module('MyApp')
.service('Client', ['Model', 'config', function (Model, config) {
var model = new Model($http, {
updateUrl: config.apiCall('api/client/:id'),
createUrl: config.apiCall('api/clients'),
getUrl: config.apiCall('api/client/:id'),
allUrl: config.apiCall('api/clients')
});
this.get = function (id) {
return model.get(id);
};
this.empty = function () {
return model.empty();
};
this.all = function () {
return model.all();
};
return this;
}])
.controller('ClientsCtrl', ['$scope', 'Client', function ($scope, Client) {
$scope.list = Client.all();
}])
.controller('ClientCtrl', ['$scope', '$stateParams', 'Client', function ($scope, $stateParams, Client) {
$scope.client = { data: { passengers: [] } };
if ($stateParams.id) {
$scope.isNew = false;
$scope.client = Client.get($stateParams.id);
}
else {
$scope.isNew = true;
$scope.client = Client.empty();
}
$scope.save = function () {
$scope.client.save();
};
$scope.reset = function () {
$scope.client = angular.copy($scope.clean);
};
$scope.isClean = function () {
return !angular.equals($scope.client, $scope.clean);
};
}]);
})();
(function(context) {
'use strict';
angular.module('MyApp')
.service('Client', ['$http', 'config', function ($http, config) {
var model = new context.Model($http, {
updateUrl: config.apiCall('api/client/:id'),
createUrl: config.apiCall('api/clients'),
getUrl: config.apiCall('api/client/:id'),
allUrl: config.apiCall('api/clients')
});
this.get = function (id) {
return model.get(id);
};
this.empty = function () {
return model.empty();
};
this.all = function () {
return model.all();
};
return this;
}])
.controller('ClientsCtrl', ['$scope', 'Client', function ($scope, Client) {
$scope.list = Client.all();
}])
.controller('ClientCtrl', ['$scope', '$stateParams', 'Client', function ($scope, $stateParams, Client) {
$scope.client = { data: { passengers: [] } };
if ($stateParams.id) {
$scope.isNew = false;
$scope.client = Client.get($stateParams.id);
}
else {
$scope.isNew = true;
$scope.client = Client.empty();
}
$scope.save = function () {
$scope.client.save();
};
$scope.reset = function () {
$scope.client = angular.copy($scope.clean);
};
$scope.isClean = function () {
return !angular.equals($scope.client, $scope.clean);
};
}]);
})(window);
(function () {
'use strict';
function formatUrl(url, data) {
var ret = url;
for (var k in data) {
ret = ret.replace(':' + k, data[k]);
}
return ret;
}
function Model($http, config) {
this.$config = config;
this.$http = $http;
}
Model.prototype.$load = function(data) {
for (var k in data) {
this[k] = data[k];
}
};
Model.prototype.$data = function() {
var ret = {};
for (var k in this) {
if (k[0] !== '$' && typeof(this[k]) !== 'function') {
ret[k] = this[k];
}
}
return ret;
};
Model.prototype.get = function (id) {
var ret = new Model(this.$http, this.$config);
ret.$loading = true;
this.$http.get(formatUrl(this.$config.getUrl, { id: id }))
.success(function (data) {
ret.$load(data);
ret.$loading = false;
});
return ret;
};
Model.prototype.save = function () {
var self = this;
this.$loading = true;
(this.id ? this.$http.put(formatUrl(this.$config.updateUrl, { id: this.id }), this.$data())
: this.$http.post(this.$config.createUrl, this.$data()))
.success(function (data) {
self.$load(data);
self.$loading = false;
});
};
Model.prototype.all = function() {
var ret = { $loading: true, data: [] };
this.$http.get(this.$config.allUrl)
.success(function (data) {
angular.forEach(data, function (ent) {
ret.data.push(ent);
});
ret.$loading = false;
});
return ret;
};
Model.prototype.empty = function () {
return new Model(this.$http, this.$config);
};
angular.module('MyApp').factory('Model', Model);
})();
(function (context) {
'use strict';
function formatUrl(url, data) {
var ret = url;
for (var k in data) {
ret = ret.replace(':' + k, data[k]);
}
return ret;
}
function Model($http, config) {
this.$config = config;
this.$http = $http;
}
Model.prototype.$load = function(data) {
for (var k in data) {
this[k] = data[k];
}
};
Model.prototype.$data = function() {
var ret = {};
for (var k in this) {
if (k[0] !== '$' && typeof(this[k]) !== 'function') {
ret[k] = this[k];
}
}
return ret;
};
Model.prototype.get = function (id) {
var ret = new Model(this.$http, this.$config);
ret.$loading = true;
this.$http.get(formatUrl(this.$config.getUrl, { id: id }))
.success(function (data) {
ret.$load(data);
ret.$loading = false;
});
return ret;
};
Model.prototype.save = function () {
var self = this;
this.$loading = true;
(this.id ? this.$http.put(formatUrl(this.$config.updateUrl, { id: this.id }), this.$data())
: this.$http.post(this.$config.createUrl, this.$data()))
.success(function (data) {
self.$load(data);
self.$loading = false;
});
};
Model.prototype.all = function() {
var ret = { $loading: true, data: [] };
this.$http.get(this.$config.allUrl)
.success(function (data) {
angular.forEach(data, function (ent) {
ret.data.push(ent);
});
ret.$loading = false;
});
return ret;
};
Model.prototype.empty = function () {
return new Model(this.$http, this.$config);
};
context.Model = Model;
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.