From 45eac85afbafe770f07e322a2647e5100c7b3042 Mon Sep 17 00:00:00 2001 From: Boris Yankov Date: Tue, 23 Oct 2012 22:57:53 +0300 Subject: [PATCH] Handlebars.js definitions and tests added --- Definitions/handlebars-1.0.d.ts | 21 +++++++++ README.md | 7 +-- Tests/handlebars-tests.ts | 77 +++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 Definitions/handlebars-1.0.d.ts create mode 100644 Tests/handlebars-tests.ts diff --git a/Definitions/handlebars-1.0.d.ts b/Definitions/handlebars-1.0.d.ts new file mode 100644 index 000000000..87b3c7caa --- /dev/null +++ b/Definitions/handlebars-1.0.d.ts @@ -0,0 +1,21 @@ +// Type definitions for Handlebars 1.0 +// Project: http://handlebarsjs.com/ +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +interface HandlebarsStatic { + registerHelper(name: string, fn: Function, inverse?: bool): void; + registerPartial(name: string, str): void; + K(); + createFrame(object); + + Exception(message: string): void; + SafeString(str: string): void; + + parse(string: string); + print(ast); + logger; + log(level, str): void; + compile(environment, options?, context?, asObject?); +} + +declare var Handlebars: HandlebarsStatic; \ No newline at end of file diff --git a/README.md b/README.md index c281acfa4..87e7101c6 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,10 @@ Complete * [async](https://github.com/caolan/async) * [Backbone.js](http://backbonejs.org/) * [Bootstrap](http://twitter.github.com/bootstrap/) +* [ember.js](http://emberjs.com/) * [Express](http://expressjs.com/) (from TypeScript samples) * [Fancybox](http://fancybox.net/) +* [Handlebars](http://handlebarsjs.com/) * [History.js](https://github.com/balupton/History.js/) * [Humane.js](http://wavded.github.com/humane-js/) (by [jmvrbanac](https://github.com/jmvrbanac)) * [Impress.js](https://github.com/bartaz/impress.js) @@ -31,13 +33,12 @@ Complete Next ---- -* ember.js -* Facebook SDK * Knockout.Mapping +* Angular.js +* Facebook SDK * jQuery.Validate * jQuery Mobile * google.visualization -* Angular.js * Meteor * PhoneGap * Isotope diff --git a/Tests/handlebars-tests.ts b/Tests/handlebars-tests.ts new file mode 100644 index 000000000..be78d887d --- /dev/null +++ b/Tests/handlebars-tests.ts @@ -0,0 +1,77 @@ +/// + +var context = { + author: { firstName: "Alan", lastName: "Johnson" }, + body: "I Love Handlebars", + comments: [{ + author: { firstName: "Yehuda", lastName: "Katz" }, + body: "Me too!" + }] +}; +Handlebars.registerHelper('fullName', (person) => { + return person.firstName + " " + person.lastName; +}); + +Handlebars.registerHelper('agree_button', () => { + return new Handlebars.SafeString( + "" + ); +}); + +var source = "

Hello, my name is {{name}}. I am from {{hometown}}. I have " + + "{{kids.length}} kids:

" + + ""; +var template = Handlebars.compile(source); +var data = { "name": "Alan", "hometown": "Somewhere, TX", + "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]}; +var result = template(data); + +Handlebars.registerHelper('link_to', (context) => { + return "" + context.body + ""; +}); + +var context2 = { posts: [{url: "/hello-world", body: "Hello World!"}] }; +var source2 = "" + +var template2 = Handlebars.compile(source2); +template2(context2); + +Handlebars.registerHelper('link_to', (title, context) => { + return "" + title + "!" +}); + +var context3 = { posts: [{url: "/hello-world", body: "Hello World!"}] }; +var source3 = '' +var template3 = Handlebars.compile(source3); +template3(context3); + +var source4 = ""; +Handlebars.registerHelper('link', (context, options) => { + return '' + context.fn(this) + ''; +}); +var template4 = Handlebars.compile(source4); +var data2 = { "people": [ + { "name": "Alan", "id": 1 }, + { "name": "Yehuda", "id": 2 } +]}; +template4(data2); + +var source5 = ""; +Handlebars.registerPartial('link', '{{name}}') +var template5 = Handlebars.compile(source5); +var data3 = { "people": [ + { "name": "Alan", "id": 1 }, + { "name": "Yehuda", "id": 2 } +]}; +template5(data3); + +Handlebars.registerHelper('list', (items, fn) => { + var out = ""; +}); +Handlebars.registerHelper('fullName', (person) => { + return person.firstName + " " + person.lastName; +}); \ No newline at end of file