Skip to content

Commit

Permalink
Merge pull request DefinitelyTyped#2077 from omidkrad/backbone-generics
Browse files Browse the repository at this point in the history
Backbone Generics (TSC 1.0)
  • Loading branch information
Diullei committed Apr 28, 2014
2 parents d8b512e + c4d0c9a commit 6834f97
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 328 deletions.
25 changes: 14 additions & 11 deletions backbone-relational/backbone-relational.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@


/// <reference path="../jquery/jquery.d.ts" />

/// <reference path="../backbone/backbone.d.ts" />

declare module Backbone {
export class RelationalModel extends Model {
static extend(properties:any, classProperties?:any):any; // do not use, prefer TypeScript's extend functionality
class RelationalModel extends Model {
/**
* Do not use, prefer TypeScript's extend functionality.
**/
//private static extend(properties:any, classProperties?:any):any;

relations:any;
subModelTypes:any;
subModelTypeAttribute:any;
Expand Down Expand Up @@ -58,7 +61,7 @@ declare module Backbone {

setRelated(related:Model):void;

setRelated(related:Collection):void;
setRelated(related:Collection<Model>):void;

getReverseRelations(model:RelationalModel):Relation;

Expand All @@ -78,15 +81,15 @@ declare module Backbone {

setKeyContents(keyContents:number[]):void;

setKeyContents(keyContents:Collection):void;
setKeyContents(keyContents:Collection<Model>):void;

onChange(model:Model, attr:any, options:any):void;

handleAddition(model:Model, coll:Collection, options:any):void;
handleAddition(model:Model, coll:Collection<Model>, options:any):void;

handleRemoval(model:Model, coll:Collection, options:any):void;
handleRemoval(model:Model, coll:Collection<Model>, options:any):void;

handleReset(coll:Collection, options:any):void;
handleReset(coll:Collection<Model>, options:any):void;

tryAddRelated(model:Model, coll:any, options:any):void;

Expand Down Expand Up @@ -135,9 +138,9 @@ declare module Backbone {

processOrphanRelations():void;

retroFitRelation(relation:RelationalModel, create:boolean):Collection;
retroFitRelation(relation:RelationalModel, create:boolean):Collection<Model>;

getCollection(type:RelationalModel, create:boolean):Collection;
getCollection(type:RelationalModel, create:boolean):Collection<Model>;

getObjectByName(name:string):any;

Expand All @@ -158,7 +161,7 @@ declare module Backbone {

update(model:RelationalModel):void;

unregister(model:RelationalModel, collection:Collection, options:any):void;
unregister(model:RelationalModel, collection:Collection<Model>, options:any):void;

reset():void;

Expand Down
147 changes: 91 additions & 56 deletions backbone/backbone-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
function test_events() {

var object = new Backbone.Events();
object.on("alert", (msg) => alert("Triggered " + msg));
object.on("alert", (eventName: string) => alert("Triggered " + eventName));

object.trigger("alert", "an event");

Expand All @@ -18,48 +18,74 @@ function test_events() {
object.off();
}

function test_models() {
class SettingDefaults extends Backbone.Model {

var Sidebar = Backbone.Model.extend({
promptColor: function () {
var cssColor = prompt("Please enter a CSS color:");
this.set({ color: cssColor });
}
});
// 'defaults' could be set in one of the following ways:

var sidebar = new Sidebar();
sidebar.on('change:color', (model, color) => $('#sidebar').css({ background: color }));
sidebar.set({ color: 'white' });
sidebar.promptColor();
defaults() {
return {
name: "Joe"
}
}

////////
constructor(attributes?: any, options?: any) {
this.defaults = <any>{
name: "Joe"
}
// super has to come last
super(attributes, options);
}

var Note = Backbone.Model.extend({
initialize: () => { },
author: () => { },
coordinates: () => { },
allowedToEdit: (account) => {
return true;
// or set it like this
initialize() {
this.defaults = <any>{
name: "Joe"
}
});

var PrivateNote = Note.extend({
}

allowedToEdit: function (account) {
return account.owns(this);
}
// same patterns could be used for setting 'Router.routes' and 'View.events'
}

});
class Sidebar extends Backbone.Model {

promptColor() {
var cssColor = prompt("Please enter a CSS color:");
this.set({ color: cssColor });
}
}

class Note extends Backbone.Model {
initialize() { }
author() { }
coordinates() { }
allowedToEdit(account: any) {
return true;
}
}

class PrivateNote extends Note {
allowedToEdit(account: any) {
return account.owns(this);
}

set(attributes: any, options?: any): Backbone.Model {
return Backbone.Model.prototype.set.call(this, attributes, options);
}
}

function test_models() {

var sidebar = new Sidebar();
sidebar.on('change:color', (model: {}, color: string) => $('#sidebar').css({ background: color }));
sidebar.set({ color: 'white' });
sidebar.promptColor();

//////////

var note = Backbone.Model.extend({
set: function (attributes, options) {
Backbone.Model.prototype.set.call(this, attributes, options);
}
});
var note = new PrivateNote();

note.get("title")
note.get("title");

note.set({ title: "March 20", content: "In his eyes she eclipses..." });

Expand All @@ -69,7 +95,7 @@ function test_models() {
class Employee extends Backbone.Model {
reports: EmployeeCollection;

constructor (options? ) {
constructor(attributes?: any, options?: any) {
super(options);
this.reports = new EmployeeCollection();
this.reports.url = '../api/employees/' + this.id + '/reports';
Expand All @@ -80,29 +106,38 @@ class Employee extends Backbone.Model {
}
}

class EmployeeCollection extends Backbone.Collection {
findByName(key) { }
class EmployeeCollection extends Backbone.Collection<Employee> {
findByName(key: any) { }
}

class Book extends Backbone.Model {
title: string;
author: string;
}

class Library extends Backbone.Collection<Book> {
model: typeof Book;
}

class Books extends Backbone.Collection<Book> { }

function test_collection() {
var Book: Backbone.Model;
var Library = Backbone.Collection.extend({
model: Book
});

var Books: Backbone.Collection;
var books = new Library();

Books.each(function (book) {
books.each(book => {
book.get("title");
});

var titles = Books.map(function (book) {
var titles = books.map(book => {
return book.get("title");
});

var publishedBooks = Books.filter(function (book) {
var publishedBooks = books.filter(book => {
return book.get("published") === true;
});

var alphabetical = Books.sortBy(function (book) {
var alphabetical = books.sortBy((book: Book): number => {
return null;
});
}
Expand All @@ -121,26 +156,26 @@ module v1Changes {

function test_listenTo() {
var model = new Employee;
var view = new Backbone.View;
var view = new Backbone.View<Employee>();
view.listenTo(model, 'invalid', () => { });
}

function test_listenToOnce() {
var model = new Employee;
var view = new Backbone.View;
var view = new Backbone.View<Employee>();
view.listenToOnce(model, 'invalid', () => { });
}

function test_stopListening() {
var model = new Employee;
var view = new Backbone.View;
var view = new Backbone.View<Employee>();
view.stopListening(model, 'invalid', () => { });
view.stopListening(model, 'invalid');
view.stopListening(model);
}
}

module modelandcollection {
module ModelAndCollection {
function test_url() {
Employee.prototype.url = () => '/employees';
EmployeeCollection.prototype.url = () => '/employees';
Expand Down Expand Up @@ -168,7 +203,7 @@ module v1Changes {
}
}

module model {
module Model {
function test_validationError() {
var model = new Employee;
if (model.validationError) {
Expand All @@ -195,17 +230,17 @@ module v1Changes {
model.destroy({
wait: true,
success: (m?, response?, options?) => { },
error: (m?, jqxhr?: JQueryXHR, options?) => { }
error: (m?, jqxhr?, options?) => { }
});

model.destroy({
success: (m?, response?, options?) => { },
error: (m?, jqxhr?: JQueryXHR) => { }
error: (m?, jqxhr?) => { }
});

model.destroy({
success: () => { },
error: (m?, jqxhr?: JQueryXHR) => { }
error: (m?, jqxhr?) => { }
});
}

Expand All @@ -220,7 +255,7 @@ module v1Changes {
wait: true,
validate: false,
success: (m?, response?, options?) => { },
error: (m?, jqxhr?: JQueryXHR, options?) => { }
error: (m?, jqxhr?, options?) => { }
});

model.save({
Expand All @@ -229,7 +264,7 @@ module v1Changes {
},
{
success: () => { },
error: (m?, jqxhr?: JQueryXHR) => { }
error: (m?, jqxhr?) => { }
});
}

Expand All @@ -240,7 +275,7 @@ module v1Changes {
}
}

module collection {
module Collection {
function test_fetch() {
var collection = new EmployeeCollection;
collection.fetch({ reset: true });
Expand All @@ -256,12 +291,12 @@ module v1Changes {
}
}

module router {
module Router {
function test_navigate() {
var router = new Backbone.Router;

router.navigate('/employees', { trigger: true });
router.navigate('/employees', true);
}
}
}
}
Loading

0 comments on commit 6834f97

Please sign in to comment.