Skip to content

Commit 6387999

Browse files
committed
Merge pull request DefinitelyTyped#4676 from craigbrett17/master
Added d.ts files for Backbone-Associations
2 parents 7bf2421 + 9a7fd63 commit 6387999

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/// <reference path="../backbone/backbone.d.ts" />
2+
/// <reference path="backbone-associations.d.ts" />
3+
4+
// borrowed from the Backbone.Associations tutorials
5+
// separated out into modules to avoid namespace clashes
6+
module Backbone.Associations.Tests {
7+
module OneToOne {
8+
class EmployeeWithManager extends Backbone.AssociatedModel {
9+
constructor(options?) {
10+
this.relations = [
11+
{
12+
type: Backbone.One, //nature of the relationship
13+
key: 'manager', // attribute of Employee
14+
relatedModel: 'Employee' //AssociatedModel for attribute key
15+
}
16+
];
17+
super(options);
18+
}
19+
20+
defaults() {
21+
return {
22+
age: 0,
23+
fname: "",
24+
lname: "",
25+
manager: null
26+
};
27+
}
28+
}
29+
}
30+
31+
module OneToMany {
32+
class Location extends Backbone.AssociatedModel {
33+
defaults() {
34+
return {
35+
add1: "",
36+
add2: null,
37+
zip: "",
38+
state: ""
39+
};
40+
}
41+
}
42+
43+
class Locations extends Backbone.Collection<Location> {
44+
comparator(c: Backbone.Model) {
45+
return c.get("Number");
46+
}
47+
}
48+
49+
class Project extends Backbone.AssociatedModel {
50+
constructor(options?) {
51+
this.relations = [
52+
{
53+
type: Backbone.Many, //nature of the relation
54+
key: 'locations', //attribute of Project
55+
collectionType: Locations, //Collection to be used.
56+
relatedModel: Location //Optional
57+
}
58+
];
59+
super(options);
60+
}
61+
62+
defaults() {
63+
return {
64+
name: "",
65+
number: 0,
66+
locations: []
67+
}
68+
}
69+
}
70+
71+
function reverseAssociationTest() {
72+
var local = new Location({ state: "Hertfordshire" });
73+
var project = new Project({ name: "The Old Pond Project" });
74+
local.set("oddRelationTo", project);
75+
var parents = project.parents;
76+
}
77+
}
78+
79+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Type definitions for Backbone-associations 0.6.4
2+
// Project: https://github.com/dhruvaray/backbone-associations/
3+
// Definitions by: Craig Brett <https://github.com/craigbrett17/>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
/// <reference path="../backbone/backbone.d.ts" />
7+
8+
declare module Backbone {
9+
export module Associations {
10+
/** Defines a 1:Many relationship type */
11+
export var Many: string;
12+
/** Defines a 1:1 relationship type */
13+
export var One: string;
14+
/** Defines a special relationship to itself */
15+
export var Self: string;
16+
17+
// these seem to be used internally, but can't see a reason not to include them just in case
18+
export var SEPARATOR: string;
19+
export function getSeparator(): any;
20+
export function setSeparator(value: any): void;
21+
export var EVENTS_BUBBLE: boolean;
22+
export var EVENTS_WILDCARD: boolean;
23+
export var EVENTS_NC: boolean;
24+
25+
interface IRelation {
26+
/** The type of model for this relationship */
27+
relatedModel: string|typeof Backbone.Associations.AssociatedModel;
28+
/** The key for this relationship on this model */
29+
key: string;
30+
// meh, no string enums in TS. Just have to trust the user not to be a fool
31+
/** The cardinality of this relationship. */
32+
type: string;
33+
/** Determines the type of collection used. If used, the relatedModel property is ignored */
34+
collectionType?: typeof Backbone.Collection|string;
35+
/** If set to true, then the attribute will not be serialized in toJSON() calls. Defaults to false */
36+
isTransient?: boolean;
37+
/** Specify remoteKey to serialize the key to a different key name in toJSON() calls. Useful in ROR nested-attributes like scenarios. */
38+
remoteKey?: string;
39+
/** the attributes to serialize when calling toJSON */
40+
serialize?: string[];
41+
/** A transformation function to convert the value before it is assigned to the key on the relatedModel */
42+
map?: (...args: any[]) => any;
43+
}
44+
45+
/** A Backbone model with special provision for handling relations to other models */
46+
export class AssociatedModel extends Backbone.Model {
47+
/** Relations with their associated model */
48+
relations: IRelation[];
49+
_proxyCalls: any;
50+
/** Reverse association lookup for objects that contain this object */
51+
parents: any[];
52+
/** Cleans up any parent relations on other AssociatedModels */
53+
cleanup(): void;
54+
}
55+
}
56+
// copies of properties also put onto the Backbone scope
57+
/** Defines a 1:Many relationship type */
58+
export var Many: string;
59+
/** Defines a 1:1 relationship type */
60+
export var One: string;
61+
/** Defines a special relationship to itself */
62+
export var Self: string;
63+
64+
// I'm sure this should be doable with imports or type aliases, but doesn't seem to work
65+
/** A Backbone model with special provision for handling relations to other models */
66+
export class AssociatedModel extends Backbone.Model {
67+
/** Relations with their associated model */
68+
relations: Associations.IRelation[];
69+
_proxyCalls: any;
70+
/** Reverse association lookup for objects that contain this object */
71+
parents: any[];
72+
/** Cleans up any parent relations on other AssociatedModels */
73+
cleanup(): void;
74+
}
75+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)