Skip to content

Commit

Permalink
Rename Razor global to Model and add global fn's to template scope
Browse files Browse the repository at this point in the history
  • Loading branch information
David Murdoch committed Jun 8, 2012
1 parent 952527f commit a10650c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
9 changes: 6 additions & 3 deletions lib/razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
var Utils = require( "./utils" ),
Parser = require( "./parser" ),
Html = require( "./html" ),
_ = require( "lodash" ),
fs = require( "fs" );


Expand Down Expand Up @@ -65,9 +66,11 @@ exports.compile = function( str, options ) {
args.push( fnStr );
var fn = Function.apply( null, args );

razor = locals;
razor.Razor = locals;
razor = {};
razor.Model = locals;
_.extend( razor, global, locals );
razor.Html = Html;
razor.JSON = JSON;

// add a RenderBody function
if( locals && locals.body ){
Expand Down Expand Up @@ -118,7 +121,7 @@ exports.compile = function( str, options ) {
},*/
hasOwn: function(name) {
return true;
return Object.prototype.hasOwnProperty.call(obj, name);
//return Object.prototype.hasOwnProperty.call(obj, name);
},
get: function(receiver, name) {
return obj[name];
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"node": ">= 0.6.3"
},
"dependencies": {
"node-proxy": "git://github.com/davidmurdoch/node-proxy.git"
"node-proxy": "git://github.com/davidmurdoch/node-proxy.git",
"lodash": "0.3.x"
},
"devDependencies": {
"qunit": "*",
Expand Down
22 changes: 11 additions & 11 deletions test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ module("Html");

test("test html.js functions", function() {
expect(12);

var string = "<string>";
var htmlstring = Html.Raw(string);
ok(htmlstring instanceof HtmlString, "Html.Raw returns HtmlString");

equal(Html.Raw(), "", "Html.Raw returns empty string for null/undefined values");

equal(Html.Encode(string), "&lt;string>", "Html.Encode(html) returns encoded HTML");

equal(Html.toString(string), "&lt;string>", "Html.toString(html) returns escaped HTML");

equal(Html.toString(htmlstring), string, "Html.toString(HtmlString) returns raw HTML");

equal(Html.toString(undefined), "", "Html.toString(undefined) returns an empty string");

equal( Html.Encode( 1.234 ), "1.234", "Html.Encode converts Numbers to strings correctly" );

equal( Html.Encode( true ), "true", "Html.Encode converts Booleans to strings correctly" );

equal( Html.Encode( {} ), "[object Object]", "Html.Encode converts Objects to strings correctly" );
equal( Html.Encode( {"foo":"bar"} ), "[object Object]", "Html.Encode converts Objects to strings correctly" );

equal( Html.Encode( [] ), "", "Html.Encode converts Arrays to strings correctly" );

equal( Html.Encode( ["uno","dos"] ), "uno,dos", "Html.Encode converts Arrays to strings correctly" );
});
7 changes: 6 additions & 1 deletion test/razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module("Razor");

test("test razor.js functions", function() {

expect(16);
expect(17);

var compile, object = {};
try{
Expand Down Expand Up @@ -105,6 +105,11 @@ test("test razor.js functions", function() {
object = {"foo":"b@r"};
equal(compile(object), "email@escaped.com value='b@r'", "Simple @ escape with additional vars");


compile = Razor.compile("@HTML.Raw( JSON.stringify(obj) )");
object = {"obj": {"foo":"bar"}};
equal(compile(object), "{\"foo\":\"bar\"}", "Can we use the JSON global?");

}
catch(e){
console.log(compile.fn);
Expand Down

0 comments on commit a10650c

Please sign in to comment.