Merge pull request #3363 from alphaleonis/Backbone.Radio

Created new type definition: Backbone.Radio
This commit is contained in:
Masahiro Wakame
2014-12-25 22:33:36 +09:00
2 changed files with 196 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
/// <reference path="backbone.radio.d.ts" />
/// <reference path="../jquery/jquery.d.ts" />
class MyCommands extends Backbone.Radio.Commands {
}
class MyRequests extends Backbone.Radio.Requests {
}
function onRender() : void {
}
function onStart(arg1 : string, arg2 : number) : number {
return 0;
}
function TestCommands() {
var myCommands = new MyCommands();
var r: Backbone.Radio.Commands;
r = myCommands.comply('render', onRender);
myCommands.command('render');
myCommands.command('start', 'pelle', 23);
r = myCommands.complyOnce('start', onStart);
r = myCommands.complyOnce('start', onStart, myCommands);
r = myCommands.comply({
'render': onRender,
'start': onStart
});
r = myCommands.complyOnce({
'render': onRender,
'start': onStart
});
myCommands.stopComplying('render');
myCommands.stopComplying(null, onStart);
myCommands.stopComplying(null, null, myCommands);
}
function TestRequests() {
var myRequests = new MyRequests();
var r: Backbone.Radio.Requests;
r = myRequests.reply('render', onRender);
var result: any;
result = myRequests.request('render');
result = myRequests.request('start', 'pelle', 23);
r = myRequests.replyOnce('start', onStart);
r = myRequests.replyOnce('start', onStart, myRequests);
r = myRequests.reply({
'render': onRender,
'start': onStart
});
r = myRequests.replyOnce({
'render': onRender,
'start': onStart
});
myRequests.stopReplying('render');
myRequests.stopReplying(null, onStart);
myRequests.stopReplying(null, null, myRequests);
}
function TestGlobalApiAndChannels() {
var authChannel = Backbone.Radio.channel('auth');
// Turn on debug mode
Backbone.Radio.DEBUG = true;
// This will log a warning to the console if it goes unhandled
authChannel.command('show:view');
// Likewise, this will too, helping to prevent memory leaks
authChannel.stopReplying('startTime');
var radio: Backbone.Radio = Backbone.Radio.tuneIn('calendar');
radio.tuneIn('calendar');
radio = Backbone.Radio.tuneOut('calendar');
Backbone.Radio.log('channelName', 'eventName', 42, true);
// Trigger 'some:event' on the settings channel
Backbone.Radio.trigger('settings', 'some:event');
authChannel.reset();
var channelName: string = authChannel.channelName;
Backbone.Radio.command('auth', 'show:view');
Backbone.Radio.comply('auth', 'show', onRender, authChannel);
Backbone.Radio.reply('auth', 'authenticate', onStart);
Backbone.Radio.request('auth', 'authenticate', 'pelle', 42);
}
+92
View File
@@ -0,0 +1,92 @@
// Type definitions for Backbone.Radio v0.8.3
// Project: https://github.com/marionettejs/backbone.radio
// Definitions by: Peter Palotas <https://github.com/alphaleonis/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../backbone/backbone.d.ts" />
declare module Backbone {
interface Radio {
tuneIn(channelName: string): Radio;
tuneOut(channelName: string): Radio;
log(channelName: string, eventName: string, ...args: any[]): void;
channel(channelName: string): Radio.Channel;
}
module Radio {
var VERSION: string;
var DEBUG: boolean;
function log(channelName: string, eventName: string, ...args: any[]): void;
function tuneIn(channelName: string): Radio;
function tuneOut(channelName: string): Radio;
function channel(channelName: string): Channel;
// Proxy functions for Commands
function command(channelName: string, commandName: string, ...args: any[]): void;
function comply(channelName: string, commandName: string, callback: (...args: any[]) => void, context?: any): Commands;
function comply(channelName: string, commands: { [key: string]: (...args: any[]) => any }, context?: any): Commands;
function complyOnce(channelName: string, commandName: string, callback: (...args: any[]) => void, context?: any): Commands;
function complyOnce(channelName: string, commands: { [key: string]: (...args: any[]) => any }, context?: any): Commands;
function stopComplying(channelName: string, commandName?: string, callback?: (...args: any[]) => void, context?: any): Commands;
// Proxy functions for Requests
function request(channelName: string, requestName: string, ...args: any[]): any;
function reply(channelName: string, requestName: string, callback: (...args: any[]) => any, context?: any): Requests;
function reply(channelName: string, commands: { [key: string]: (...args: any[]) => any }, context?: any): Requests;
function replyOnce(channelName: string, requestName: string, callback: (...args: any[]) => any, context?: any): Requests;
function replyOnce(channelName: string, commands: { [key: string]: (...args: any[]) => any }, context?: any): Requests;
function stopReplying(channelName: string, commandName?: string, callback?: (...args: any[]) => any, context?: any): Requests;
// Proxy functions for Backbone.Events
function on(channelName: string, eventName: string, callback?: Function, context?: any): any;
function off(channelName: string, eventName?: string, callback?: Function, context?: any): any;
function trigger(channelName: string, eventName: string, ...args: any[]): any;
function bind(channelName: string, eventName: string, callback: Function, context?: any): any;
function unbind(channelName: string, eventName?: string, callback?: Function, context?: any): any;
function once(channelName: string, events: string, callback: Function, context?: any): any;
function listenTo(channelName: string, object: any, events: string, callback: Function): any;
function listenToOnce(channelName: string, object: any, events: string, callback: Function): any;
function stopListening(channelName: string, object?: any, events?: string, callback?: Function): any;
class Commands {
command(commandName: string, ...args: any[]): void;
comply(commandName: string, callback: (...args: any[]) => void, context?: any): Commands;
comply(commands: { [key: string]: (...args: any[]) => any }, context?: any): Commands;
complyOnce(commands: { [key: string]: (...args: any[]) => any }, context?: any): Commands;
complyOnce(commandName: string, callback: (...args: any[]) => void, context?: any): Commands;
stopComplying(commandName?: string, callback?: (...args: any[]) => void, context?: any): Commands;
}
class Requests {
request(requestName: string, ...args: any[]): any;
reply(requestName: string, callback: (...args: any[]) => any, context?: any): Requests;
reply(commands: { [key: string]: (...args: any[]) => any }, context?: any): Requests;
replyOnce(requestName: string, callback: (...args: any[]) => any, context?: any): Requests;
replyOnce(commands: { [key: string]: (...args: any[]) => any }, context?: any): Requests;
stopReplying(commandName?: string, callback?: (...args: any[]) => any, context?: any): Requests;
}
class Channel extends Backbone.Events implements Commands, Requests {
channelName: string;
reset(): Channel;
// Radio.Commands
command(commandName: string, ...args: any[]): void;
comply(commandName: string, callback: (...args: any[]) => void, context?: any): Commands;
comply(commands: { [key: string]: (...args: any[]) => any }, context?: any): Commands;
complyOnce(commandName: string, callback: (...args: any[]) => void, context?: any): Commands;
complyOnce(commands: { [key: string]: (...args: any[]) => any }, context?: any): Commands;
stopComplying(commandName?: string, callback?: (...args: any[]) => void, context?: any): Commands;
// Radio.Requests
request(requestName: string, ...args: any[]): any;
reply(requestName: string, callback: (...args: any[]) => any, context?: any): Requests;
reply(commands: { [key: string]: (...args: any[]) => any }, context?: any): Requests;
replyOnce(requestName: string, callback: (...args: any[]) => any, context?: any): Requests;
replyOnce(commands: { [key: string]: (...args: any[]) => any }, context?: any): Requests;
stopReplying(commandName?: string, callback?: (...args: any[]) => any, context?: any): Requests;
}
}
}