• This repository has been archived on 14/Jul/2021
  • Stars
    star
    137
  • Rank 266,121 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A model layer for AngularJS inspired by Backbone.Model

ActiveRecord for AngularJS

angular-activerecord is a Backbone.Model inspired modellayer for AngularJS.

Build Status

API Documentation

Differences compared to Backbone.Model

  • No attributes property.
  • Because the properties and methods are on the same level the ActiveRecord methods & config-properties are prefixed with "$" to prevent naming collisions.
  • Stripped out functionality that is provided by angular)
    • No getter/setter methods. (Angular has $scope.$watch)
    • No event system. (Angular has $scope.$emit)
    • No dependency on underscore. (angular.extend, angular.isFunction, etc)
    • No dependency on jQuery. (Angular has $http)
    • No Collection class. (Angular works with plain javascript array's)
  • Added static fetchOne(id) and fetchAll() class-methods.
  • Added read & write filtering of properties through angular filters.

Goals / Features (compared to ngResource)

  • Extendable OOP designed models (instance methods per type)
  • Enable parsing the response.
  • Allow default values.
  • Allow alternative backends.
  • Allow alternative url schemes (like a .json suffix)
  • Minimal configuration (only a $urlRoot), the json-object from the rest-api is the spec.

Examples

Defining a model

module('myApp', ['ActiveRecord']); // Add "ActiveRecord" as module dependency.

module('myApp').factory('Task', function (ActiveRecord) {

   return ActiveRecord.extend({

   	// Rest API configuration for retrieving and saving tasks.
   	$urlRoot: '/api/tasks',

   	// Optional defaults
   	$defaults: {
   		title: 'Untitled',
   		estimate: ''
   	},

   	// optional named constructor (Shows "Task" as the type in a console.log)
   	$constructor: function Task(properties) {
   		this.$initialize.apply(this, arguments)
   	},

   	// An example method for task instances
   	/**
   	 * Return the estimate in hours
   	 * @return {Number}
   	 */
   	estimateInHours: function () {
   		var value = parseFloat(this.estimate);
   		if (isNaN(value)) {
   			return 0.0;
   		}
   		return value / 3600;
   	}
   });

Fetching and saving data.

module('myApp').controller('TaskCtrl', function ($scope, Task, $document) {

	Task.fetchOne(7).then(function (task7) { // Fetches '/api/tasks/7'
		$scope.task = task7;
		$document.title = task7.title  + ' - MyApp';
	});

	/**
	 * @param {Task} task
	 */
	$scope.saveTask = function (task) {
		$scope.spinnerVisible = true;
		task.$save().then(function () {
			$scope.successVisible = true;
		}).catch(function (error) {
			$scope.error = error;
		}).finally(function () {
			$scope.spinnerVisible = false;
		});
	};
});

Loading models via ngRoute

module('myApp', ['ngRoute']).config(function ($routeProvider) {
	$routeProvider
		.when('/tasks', {
			templateUrl: 'tasks.html',
			controller: 'TaskListCtrl',
			resolve: {
				tasks: function (Task) {
					return Task.fetchAll();
				}
			}
		})
		.when('/tasks/:taskId', {
			templateUrl: 'task.html',
			controller: 'TaskCtrl',
			resolve: {
				task: function ($routeParams, Task) {
					return Task.fetchOne($routeParams.taskId);
				}
			}
		});
});

module('myApp').controller('TaskCtrl', function ($scope, task) {
	$scope.task = task;
});

More Repositories

1

pixi-inspector

Devtools for PixiJS
TypeScript
395
star
2

eslint-plugin-only-warn

Downgrade errors to warnings
JavaScript
157
star
3

svelte-preprocess-react

Seamlessly use React components inside a Svelte app
TypeScript
128
star
4

angular-keyboard

Keyboard behavior for AngularJS Webapps
JavaScript
37
star
5

tvkit

Proxy to run a modern dev server in old browsers
JavaScript
25
star
6

page-transitions-in-svelte

Using page transitions Today (with Svelte)
Svelte
23
star
7

r8168dl

Was used for the now deprecated r8168dl.appspot.com, for newer drivers go to: http://github.com/mtorromeo/r8168
Hack
22
star
8

svelte-cannon

Declarative Svelte components for the cannon-es physics engine
Svelte
20
star
9

multiplayer-dice-game

TypeScript
16
star
10

framebuffer

low level access to the linux frame buffer from go
Go
13
star
11

swagger-explained

Interactive documentation of the swagger.json
TypeScript
10
star
12

svelte-ssr-go

Render svelte pages with embedded v8 in go
Go
6
star
13

svelte-project-template

An opinionated starter-template for SvelteKit projects
TypeScript
5
star
14

svelte-outin

<transition mode="out-in"> for Svelte
TypeScript
3
star
15

beurtbalkje

Connection queue proxy for restarting services
Go
2
star
16

blender-elements

Bringing the UI controls from Blender to the web
JavaScript
2
star
17

flowcart

Svelte
1
star
18

clock

Clock software in Golang for Raspberry Pi
Go
1
star
19

sledgehammer_cakephp

Use CakePHP in an Sledgehammer project
PHP
1
star
20

micropython-livereload

LiveReload for Microcontrollers
Python
1
star
21

sledgehammer_forms

Een SledgeHammer module om (web)formulieren mee te genereren
PHP
1
star
22

sledgehammer_beanstalk

Sledgehammer module for accessing the Beanstalk REST API
PHP
1
star
23

dust.dom.js

Add support for adding DOM hooks within a dust.helpers
JavaScript
1
star
24

sledgehammer_geoip

SledgeHammer module voor het bepalen van het Land o.b.v. het IP adres.
PHP
1
star
25

sledgehammer_webcomponents

Collectie componenten voor het SledgeHammer Framework
PHP
1
star
26

t-shirt-idea

HTML
1
star