From a24112740904688bc2635ded719dad0178995218 Mon Sep 17 00:00:00 2001 From: Craig Brett Date: Fri, 19 Jun 2015 15:43:08 +0100 Subject: [PATCH 1/2] Added d.ts files for Backbone-Associations --- .../backbone-associations-tests.ts | 79 +++++++++++++++++++ .../backbone-associations-tests.ts.tscparams | 1 + .../backbone-associations.d.ts | 75 ++++++++++++++++++ .../backbone-associations.d.ts.tscparams | 1 + 4 files changed, 156 insertions(+) create mode 100644 Backbone-Associations/backbone-associations-tests.ts create mode 100644 Backbone-Associations/backbone-associations-tests.ts.tscparams create mode 100644 Backbone-Associations/backbone-associations.d.ts create mode 100644 Backbone-Associations/backbone-associations.d.ts.tscparams diff --git a/Backbone-Associations/backbone-associations-tests.ts b/Backbone-Associations/backbone-associations-tests.ts new file mode 100644 index 000000000..d25ddf17e --- /dev/null +++ b/Backbone-Associations/backbone-associations-tests.ts @@ -0,0 +1,79 @@ +/// +/// + +// borrowed from the Backbone.Associations tutorials +// separated out into modules to avoid namespace clashes +module Backbone.Associations.Tests { + module OneToOne { + class EmployeeWithManager extends Backbone.AssociatedModel { + constructor(options?) { + this.relations = [ + { + type: Backbone.One, //nature of the relationship + key: 'manager', // attribute of Employee + relatedModel: 'Employee' //AssociatedModel for attribute key + } + ]; + super(options); + } + + defaults() { + return { + age: 0, + fname: "", + lname: "", + manager: null + }; + } + } + } + + module OneToMany { + class Location extends Backbone.AssociatedModel { + defaults() { + return { + add1: "", + add2: null, + zip: "", + state: "" + }; + } + } + + class Locations extends Backbone.Collection { + comparator(c: Backbone.Model) { + return c.get("Number"); + } + } + + class Project extends Backbone.AssociatedModel { + constructor(options?) { + this.relations = [ + { + type: Backbone.Many, //nature of the relation + key: 'locations', //attribute of Project + collectionType: Locations, //Collection to be used. + relatedModel: Location //Optional + } + ]; + super(options); + } + + defaults() { + return { + name: "", + number: 0, + locations: [] + } + } + } + + function reverseAssociationTest() { + var local = new Location({ state: "Hertfordshire" }); + var project = new Project({ name: "The Old Pond Project" }); + local.set("oddRelationTo", project); + var parents = project.parents; + } + } + +} \ No newline at end of file diff --git a/Backbone-Associations/backbone-associations-tests.ts.tscparams b/Backbone-Associations/backbone-associations-tests.ts.tscparams new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Backbone-Associations/backbone-associations-tests.ts.tscparams @@ -0,0 +1 @@ + diff --git a/Backbone-Associations/backbone-associations.d.ts b/Backbone-Associations/backbone-associations.d.ts new file mode 100644 index 000000000..2a9ecf779 --- /dev/null +++ b/Backbone-Associations/backbone-associations.d.ts @@ -0,0 +1,75 @@ +// Type definitions for Backbone-associations 0.6.4 +// Project: https://github.com/dhruvaray/backbone-associations/ +// Definitions by: Craig Brett +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Backbone { + export module Associations { + /** Defines a 1:Many relationship type */ + export var Many: string; + /** Defines a 1:1 relationship type */ + export var One: string; + /** Defines a special relationship to itself */ + export var Self: string; + + // these seem to be used internally, but can't see a reason not to include them just in case + export var SEPARATOR: string; + export function getSeparator(): any; + export function setSeparator(value: any): void; + export var EVENTS_BUBBLE: boolean; + export var EVENTS_WILDCARD: boolean; + export var EVENTS_NC: boolean; + + interface IRelation { + /** The type of model for this relationship */ + relatedModel: string|typeof Backbone.Associations.AssociatedModel; + /** The key for this relationship on this model */ + key: string; + // meh, no string enums in TS. Just have to trust the user not to be a fool + /** The cardinality of this relationship. */ + type: string; + /** Determines the type of collection used. If used, the relatedModel property is ignored */ + collectionType?: typeof Backbone.Collection|string; + /** If set to true, then the attribute will not be serialized in toJSON() calls. Defaults to false */ + isTransient?: boolean; + /** Specify remoteKey to serialize the key to a different key name in toJSON() calls. Useful in ROR nested-attributes like scenarios. */ + remoteKey?: string; + /** the attributes to serialize when calling toJSON */ + serialize?: string[]; + /** A transformation function to convert the value before it is assigned to the key on the relatedModel */ + map?: (...args: any[]) => any; + } + + /** A Backbone model with special provision for handling relations to other models */ + export class AssociatedModel extends Backbone.Model { + /** Relations with their associated model */ + relations: IRelation[]; + _proxyCalls: any; + /** Reverse association lookup for objects that contain this object */ + parents: any[]; + /** Cleans up any parent relations on other AssociatedModels */ + cleanup(): void; + } + } + // copies of properties also put onto the Backbone scope + /** Defines a 1:Many relationship type */ + export var Many: string; + /** Defines a 1:1 relationship type */ + export var One: string; + /** Defines a special relationship to itself */ + export var Self: string; + + // I'm sure this should be doable with imports or type aliases, but doesn't seem to work + /** A Backbone model with special provision for handling relations to other models */ + export class AssociatedModel extends Backbone.Model { + /** Relations with their associated model */ + relations: Associations.IRelation[]; + _proxyCalls: any; + /** Reverse association lookup for objects that contain this object */ + parents: any[]; + /** Cleans up any parent relations on other AssociatedModels */ + cleanup(): void; + } +} \ No newline at end of file diff --git a/Backbone-Associations/backbone-associations.d.ts.tscparams b/Backbone-Associations/backbone-associations.d.ts.tscparams new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/Backbone-Associations/backbone-associations.d.ts.tscparams @@ -0,0 +1 @@ + From 9a7fd63e89244da016ca14f4522b60714a445094 Mon Sep 17 00:00:00 2001 From: Craig Brett Date: Tue, 23 Jun 2015 14:47:50 +0100 Subject: [PATCH 2/2] Handling case sensativity --- .../backbone-associations-tests.ts | 0 .../backbone-associations-tests.ts.tscparams | 0 .../backbone-associations.d.ts | 0 .../backbone-associations.d.ts.tscparams | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename {Backbone-Associations => backbone-associations}/backbone-associations-tests.ts (100%) rename {Backbone-Associations => backbone-associations}/backbone-associations-tests.ts.tscparams (100%) rename {Backbone-Associations => backbone-associations}/backbone-associations.d.ts (100%) rename {Backbone-Associations => backbone-associations}/backbone-associations.d.ts.tscparams (100%) diff --git a/Backbone-Associations/backbone-associations-tests.ts b/backbone-associations/backbone-associations-tests.ts similarity index 100% rename from Backbone-Associations/backbone-associations-tests.ts rename to backbone-associations/backbone-associations-tests.ts diff --git a/Backbone-Associations/backbone-associations-tests.ts.tscparams b/backbone-associations/backbone-associations-tests.ts.tscparams similarity index 100% rename from Backbone-Associations/backbone-associations-tests.ts.tscparams rename to backbone-associations/backbone-associations-tests.ts.tscparams diff --git a/Backbone-Associations/backbone-associations.d.ts b/backbone-associations/backbone-associations.d.ts similarity index 100% rename from Backbone-Associations/backbone-associations.d.ts rename to backbone-associations/backbone-associations.d.ts diff --git a/Backbone-Associations/backbone-associations.d.ts.tscparams b/backbone-associations/backbone-associations.d.ts.tscparams similarity index 100% rename from Backbone-Associations/backbone-associations.d.ts.tscparams rename to backbone-associations/backbone-associations.d.ts.tscparams