Skip to content

Commit 8e3f0eb

Browse files
committed
First sort-of-working commit
0 parents  commit 8e3f0eb

36 files changed

+54035
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2012 Jim Hoskins
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a
4+
copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included
12+
in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Parse for AngularJS
2+
3+
_This is pre-alpha/actively developed. There are no guarantees of
4+
stability, but you are welcome to play around and submit issues_
5+
6+
angular-parse is an [AngularJS](http://angularjs.org) module for
7+
interacting with the [Parse](http://parse.com) [REST
8+
API](https://parse.com/docs/rest). It *does not* utlize the [Parse
9+
JavaScript API](https://parse.com/docs/js_guide) but instead is built
10+
from (mostly) scratch. The reason is the existing Parse JavaScript API
11+
is not ideal for AngularJS applications.
12+
13+
# Why Angular-Parse
14+
15+
There are a few things that are not ideal about the existing Parse
16+
JavaScript API in AngularJS. The existing API is modeled after [Backbone
17+
Models](http://backbonejs.org/#Model) and the main problem is setters
18+
are used instead of object properties. `instance.set('property', 'value')`
19+
doesn't really fit well with things like `ng-model`
20+
21+
Instead, angular-parse is based loosely on [Spine
22+
Models](http://spinejs.com/docs/models) where properties directly
23+
defined on the object are used. To facilitate this, when defining a
24+
model, it is "configured" by supplying the class name (as defined in
25+
Parse) as well as which properties are part of that class.
26+
27+
Angular-parse also uses promises for any methods making network calls.
28+
29+
## Getting started
30+
31+
Include the JavaScript file
32+
33+
```html
34+
<!-- Include AngularJS -->
35+
<script src="path/to/angular-parse.js"></script>
36+
```
37+
38+
Make sure to add `"Parse"` as a dependency of your main module
39+
40+
```javascript
41+
var app = angular.module("YourApp", ["Parse"])
42+
```
43+
44+
Angular-parse also requires you provide the value "ParseConfig" as an
45+
object with the following format
46+
47+
```javascript
48+
app.value("ParseConfig", {
49+
applicationId: "YOUR PARSE APPLICATION ID",
50+
apiKey: "YOUR PARSE REST API KEY"
51+
})
52+
```
53+
54+
## Defining Models
55+
56+
You can define models by extending Parse.Model. You must call configure
57+
on the class and pass it the Parse class name, and the name of any
58+
attributes of that class
59+
60+
Using CoffeeScript:
61+
```coffeescript
62+
app.factory 'Car', (Parse) ->
63+
class Car extends Parse.model
64+
@configure "Car", "make", "model", "year"
65+
66+
@customClassMethod: (arg) ->
67+
# add custom class methods like this
68+
69+
customInstanceMethod: (arg) ->
70+
# add custom instance methods like this
71+
```
72+
73+
Using JavaScript:
74+
```javascript
75+
// Not implemented yet, sorry
76+
```
77+
78+
79+
## Using Models
80+
81+
A model acts much the same as a normal JavaScript object with a
82+
constructor
83+
84+
### Creating a new instance
85+
86+
You can create a new instance by using `new`. Any attributes passed in
87+
will be set on the instance. This does not save it to parse, that must
88+
be done with `.save()`. The save method returns a promise, which is
89+
fulfilled with the instance itself.
90+
91+
```javascript
92+
var car = new Car({
93+
make: "Scion",
94+
model: "xB",
95+
year: 2008
96+
});
97+
98+
car.isNew() === true;
99+
car.objectId == null;
100+
101+
car.save().then(function (_car) {
102+
_car === car;
103+
car.isNew() === false;
104+
car.objectId === "...aParseId";
105+
car.createdAt === "...aDateString";
106+
car.updatedAt === "...aDateString";
107+
}
108+
```
109+
110+
If the object has an objectId, it will be updated properly, and will not
111+
create a new instance. `save()` can be used either for new or existing
112+
records.
113+
114+
### Getting an instance By Id
115+
116+
The `find` method on your model class takes an objectId, and returns a
117+
promise that will be fulfilled with your instance if it exists.
118+
119+
120+
```javascript
121+
Car.find("someObjectId").then(function (car) {
122+
car.objectId === "someObjectId";
123+
})
124+
```
125+
126+
### Destroying an instance
127+
128+
The destroy method on an instance will destroy it set destroyed to true
129+
and set the item's objectId to null
130+
131+
```javascript
132+
Car.find("someObjectId").then(function (car) {
133+
car.objectId === "someObjectId";
134+
135+
car.destroy().then(function (_car) {
136+
car === _car;
137+
car.destroyed === true;
138+
car.isNew() === true;
139+
car.objectId === null;
140+
})
141+
})
142+
```
143+
144+
### Contributing
145+
146+
Pull requests and issues are welcome.

angular-parse.js

+124
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var app = angular.module('parseExample', ['parse']);

example/app.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var app = angular.module('parseExample', ['Parse']);
2+
3+
app.controller('demo', function ($q, Parse) {
4+
5+
6+
Parse.Model.configure("Car", "make", "model")
7+
window.c = new Parse.Model
8+
x = c.save()
9+
x.then(function(){
10+
alert('win')
11+
}, function () {
12+
alert('lose')
13+
})
14+
15+
window.x = x
16+
17+
})

example/coffee/app.coffee

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
app = angular.module 'CarDealer', ['Parse']
2+
3+
app.config ($routeProvider) ->
4+
$routeProvider
5+
.when("/", controller: "CarList", templateUrl: "partials/list.html")
6+
.when("/new-car", controller: "CarForm", templateUrl: "partials/form.html")
7+
.when("/cars/:id", controller: "CarDetail", templateUrl: "partials/detail.html")
8+
.when("/edit-car/:id", controller: "CarForm", templateUrl: "partials/form.html")
9+
.otherwise(redirectTo: "/")
10+
11+
app.value 'ParseConfig',
12+
applicationId: "OczAR6VkElaVe5012kOHccwSJSdd3xtU1jBXRBDK"
13+
apiKey: "OtKOw3a45SBJL7AesN2kymOJwD0cG9YTdaOAi9BF"
14+
15+
app.factory 'Car', (Parse) ->
16+
class Car extends Parse.Model
17+
@configure 'Car', 'make', 'model', 'year'
18+
19+
20+
app.controller 'CarList', ($scope, Car) ->
21+
$scope.load = ->
22+
Car.query().then (cars) ->
23+
$scope.cars = cars
24+
25+
$scope.destroy = (car) ->
26+
car.destroy().then -> $scope.load()
27+
28+
$scope.load()
29+
30+
app.controller 'CarDetail', ($scope, $routeParams, Car) ->
31+
id = $routeParams.id
32+
Car.find(id).then (car) ->
33+
$scope.car = car
34+
35+
app.controller 'CarForm', ($scope, $location, $routeParams, Car) ->
36+
id = $routeParams.id
37+
38+
if id
39+
Car.find(id).then (car) ->
40+
$scope.car = car
41+
else
42+
$scope.car = new Car
43+
44+
$scope.save = ->
45+
$scope.car?.save().then (car) ->
46+
$location.path("/")
47+

example/index.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html ng-app="CarDealer">
3+
<head>
4+
<title>Parse Example</title>
5+
<link rel="stylesheet" href="./lib/bootstrap/css/bootstrap.css">
6+
</head>
7+
<body >
8+
<div class="container">
9+
<h1>Parse</h1>
10+
<a href="#/new-car" class="btn">
11+
New
12+
</a>
13+
<div ng-view></div>
14+
</div>
15+
<script src="../lib/angular/angular.js"></script>
16+
<script src="../angular-parse.js"></script>
17+
<script src="./js/app.js"></script>
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)