From f41926c32b76be8938794f22dd09b866f2077a87 Mon Sep 17 00:00:00 2001 From: Pavel Zet Date: Wed, 26 Feb 2014 16:38:45 +0200 Subject: [PATCH] v0.2.2 --- xt.js | 345 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 193 insertions(+), 152 deletions(-) diff --git a/xt.js b/xt.js index 62c00d4..1936569 100644 --- a/xt.js +++ b/xt.js @@ -17,10 +17,10 @@ * - https://github.com/zetorama/requirejs-xt */ -define(['require', 'module', 'text', 'deferred'], function (require, module, text, dfr) { +define(['module', 'text', 'deferred'], function (module, text, dfr) { 'use strict'; - var VERSION = '0.2.0', + var VERSION = '0.2.2', loading = {}, loaded = {}, extend = function(obj, ext) { @@ -46,7 +46,7 @@ define(['require', 'module', 'text', 'deferred'], function (require, module, tex }; })(), Deferred = dfr.Deferred || dfr, - rx = { + rxMap = { template: /((?:.|\s)*?)<\/x-template>/g, wrapper: /((?:.|\s)*?)<\/x-wrapper>/g, require: //g, @@ -74,7 +74,7 @@ define(['require', 'module', 'text', 'deferred'], function (require, module, tex parseAttributes = function(str) { var result = {}, - attrs = str.split(rx.spaces), + attrs = str.split(rxMap.spaces), k, attr, parts, key, val; for (k = attrs.length; k--;) { @@ -82,7 +82,7 @@ define(['require', 'module', 'text', 'deferred'], function (require, module, tex parts = attr.split('='); key = parts.shift(); - val = parts.join('=').replace(rx.quotes, ''); + val = parts.join('=').replace(rxMap.quotes, ''); result[key] = trim.call(val); } @@ -100,7 +100,7 @@ define(['require', 'module', 'text', 'deferred'], function (require, module, tex superAliasDefault: 'super', extend: function(obj) { - return extend(linkPrototype(plugin), obj); + return extend(linkPrototype(this), obj); }, register: function(template, required) { @@ -112,22 +112,42 @@ define(['require', 'module', 'text', 'deferred'], function (require, module, tex }, compile: function(text, name, template) { + var plugin = this, + compiled = text; + compiled.partial = function(partialName) { + return plugin.get() + }; + return text; }, - get: function(url) { - return loaded[url]; + get: function(url, partialName) { + var plugin = this, + template = loaded[url], + result; + + if (!template) { + throw 'Requested template file was not loaded: ' + template.id; + } + + partialName || (partialName = plugin.templateNameDefault); + result = template.compiled[partialName]; + + if (!result) { + throw 'Requested template "' + partialName + '" is not defined in file ' + template.id; + } + + return result; }, - parseName: function(name, req) { - var parts = String(name).split('!', 2), - path = this.addExtension(parts[0]), - options = parts[1] || ''; + parseName: function(name) { + var parts = String(name).split(':', 2), + file = this.addExtension(parts[0]), + partialName = parts[1] || ''; return { - path: path, - url: req.toUrl(path), - partial: options + file: file, + partial: partialName }; }, @@ -137,92 +157,26 @@ define(['require', 'module', 'text', 'deferred'], function (require, module, tex return hasExtension ? path : [path, this.extension].join('.'); }, - parseContent: function(content) { - var plugin = this, - data = { - partials: {}, - wrappers: {}, - wrapMap: {}, - incDeps: [], - incAliases: [], - reqDeps: [], - reqAliases: [], - extendFrom: false - }, - magicPosition = content.indexOf('