Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Commit dd251f5

Browse files
committed
Merge pull request #41 from zpbx/adapter
Update adapter files, release new version.
2 parents 7fc48ef + b6bc5db commit dd251f5

10 files changed

Lines changed: 92 additions & 62 deletions

File tree

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "underscore.ext",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"homepage": "https://github.com/CMUI/underscore.ext",
55
"authors": [
66
"cssmagic"
@@ -29,7 +29,7 @@
2929
"src"
3030
],
3131
"dependencies": {
32-
"action": "0.2.1",
32+
"action": "~0.2.2",
3333
"underscore.template": "*",
3434
"underscore": "^1.6.0",
3535
"zepto": "^1.1.3"

build.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ mkdir dist
66

77
# normal release
88
cat \
9-
src/adapter-trad/_intro.js \
10-
src/adapter-trad/_var.js \
9+
src/adapter-dist-trad/_intro.js \
10+
src/adapter-dist-trad/var.js \
11+
src/adapter-dist-trad/_defense.js \
1112
src/core.js \
1213
src/str-backup.js \
1314
src/str.js \
@@ -22,14 +23,14 @@ cat \
2223
bower_components/underscore.template/src/underscore.template.js \
2324
src/adapter-mod-template/config.js \
2425
src/adapter-mod-template/_outro.js \
25-
src/adapter-trad/_outro.js \
26+
src/adapter-dist-trad/_outro.js \
2627
> \
2728
dist/underscore.ext.js
2829

2930
# cmd release
3031
cat \
31-
src/adapter-cmd/_intro.js \
32-
src/adapter-cmd/_var.js \
32+
src/adapter-dist-cmd/_intro.js \
33+
src/adapter-dist-cmd/var.js \
3334
src/core.js \
3435
src/str-backup.js \
3536
src/str.js \
@@ -44,7 +45,7 @@ cat \
4445
bower_components/underscore.template/src/underscore.template.js \
4546
src/adapter-mod-template/config.js \
4647
src/adapter-mod-template/_outro.js \
47-
src/adapter-cmd/_outro.js \
48+
src/adapter-dist-cmd/_outro.js \
4849
> \
4950
dist/underscore.ext.cmd.js
5051

dist/underscore.ext.js

Lines changed: 80 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -610,14 +610,15 @@ void function (window, _ext) {
610610
}(window, _ext)
611611

612612
//////////////////// action ////////////////////
613-
//include and wrap external lib: action.js
613+
//include and wrap external module: action.js
614614

615615
void function (window, _ext) {
616616
'use strict'
617617

618618
/**
619619
* Action.js - Easy and lazy solution for click-event-binding.
620-
* (https://github.com/cssmagic/action.js)
620+
* Released under the MIT license.
621+
* https://github.com/cssmagic/action.js
621622
*/
622623
var action = function () {
623624
'use strict'
@@ -628,6 +629,20 @@ var action = function () {
628629
var SELECTOR = '[data-action]'
629630
var _actionList = {}
630631

632+
//util
633+
function _getActionName($elem) {
634+
var result = $elem.data('action') || ''
635+
if (!result) {
636+
var href = $.trim($elem.attr('href'))
637+
if (href && href.indexOf('#') === 0) result = href
638+
}
639+
return _formatActionName(result)
640+
}
641+
function _formatActionName(s) {
642+
var result = s ? $.trim(String(s)).replace(/^[#!]+/, '') : ''
643+
return $.trim(result)
644+
}
645+
631646
function _init() {
632647
var $wrapper = $(document.body || document.documentElement)
633648
$wrapper.on('click', SELECTOR, function (ev) {
@@ -645,19 +660,6 @@ var action = function () {
645660
}
646661
})
647662
}
648-
649-
function _getActionName($elem) {
650-
var result = $elem.data('action') || ''
651-
if (!result) {
652-
var href = $.trim($elem.attr('href'))
653-
if (href && href.indexOf('#') === 0) result = href
654-
}
655-
return _formatActionName(result)
656-
}
657-
function _formatActionName(s) {
658-
var result = s ? $.trim(s + '').replace(/^[#!]+/, '') : ''
659-
return $.trim(result)
660-
}
661663
function _handle(actionName, context) {
662664
var fn = _actionList[actionName]
663665
if ($.isFunction(fn)) {
@@ -728,39 +730,42 @@ var action = function () {
728730
}(window, _ext)
729731

730732
//////////////////// template ////////////////////
731-
//front-end template fetching, caching and rendering
733+
//include and wrap external module: underscore.template
732734

733735
void function (window, _ext) {
734736
'use strict'
735737

738+
/**
739+
* Underscore.template - More APIs for Underscore's template engine.
740+
* Released under the MIT license.
741+
* https://github.com/cssmagic/underscore.template
742+
*/
743+
var template = function () {
744+
'use strict'
745+
736746
//namespace
737747
var template = {}
738748

739749
//config
740-
var _config = {
741-
//for jedi 1.0
742-
needStripCommentTag: true,
743-
744-
//compatible with ejs
745-
interpolate : /<%-([\s\S]+?)%>/g,
746-
escape : /<%=([\s\S]+?)%>/g,
747-
748-
//to avoid use `with` in compiled templates
749-
//see: https://github.com/cssmagic/blog/issues/4
750-
variable: 'data'
751-
}
752-
var PREFIX_TEMPLATE = 'template-'
750+
var ELEM_ID_PREFIX = 'template-'
753751

754752
//cache
755753
var _cacheTemplate = {}
756754
var _cacheCompiledTemplate = {}
757755

758756
//util
759757
function _toTemplateId(id) {
760-
return String(id).replace(PREFIX_TEMPLATE, '')
758+
//`#template-my-tpl-001` -> `my-tpl-001`
759+
// `template-my-tpl-001` -> `my-tpl-001`
760+
// `my-tpl-001` -> `my-tpl-001`
761+
id = id ? _.str.trim(id).replace(/^[#!]+/, '') : ''
762+
return _.str.trim(id).replace(ELEM_ID_PREFIX, '')
761763
}
762764
function _toElementId(id) {
763-
return _.str.startsWith(id, PREFIX_TEMPLATE) ? id : PREFIX_TEMPLATE + id
765+
//`template-my-tpl-001` -> `template-my-tpl-001`
766+
// `my-tpl-001` -> `template-my-tpl-001`
767+
id = id ? _.str.trim(id) : ''
768+
return _.str.startsWith(id, ELEM_ID_PREFIX) ? id : ELEM_ID_PREFIX + id
764769
}
765770
function _stripCommentTag(str) {
766771
str = String(str)
@@ -772,25 +777,33 @@ void function (window, _ext) {
772777
}
773778
//get template by id (of dummy script element in html)
774779
function _getTemplateById(id) {
775-
if (!id || !_.isString(id)) return false
780+
if (!id) return false
776781
var result
777-
var idElement = _toElementId(id)
778-
var elem = document.getElementById(idElement)
779-
if (!elem) {
780-
console.error('Element "#' + idElement + '" not found!')
781-
} else {
782+
var elementId = _toElementId(String(id))
783+
var elem = document.getElementById(elementId)
784+
if (elem) {
782785
var str = _.str.trim(elem.innerHTML)
783-
if (!str) {
784-
console.error('Element "#' + idElement + '" is empty!')
785-
} else {
786+
if (str) {
786787
//strip html comment tag wrapping template code
787-
if (_.templateSettings.needStripCommentTag) str = _stripCommentTag(str)
788+
//especially for jedi 1.0 (https://github.com/baixing/jedi)
789+
if (_.templateSettings.shouldUnwrapCommentTag) str = _stripCommentTag(str)
790+
788791
if (_isTemplateCode(str)) {
789792
result = str
790793
} else {
791-
console.error('Template code in element "#' + idElement + '" is no valid!')
794+
/** DEBUG_INFO_START **/
795+
console.warn('[Template] Template code in element "#' + elementId + '" is invalid!')
796+
/** DEBUG_INFO_END **/
792797
}
798+
} else {
799+
/** DEBUG_INFO_START **/
800+
console.warn('[Template] Element "#' + elementId + '" is empty!')
801+
/** DEBUG_INFO_END **/
793802
}
803+
} else {
804+
/** DEBUG_INFO_START **/
805+
console.warn('[Template] Element "#' + elementId + '" not found!')
806+
/** DEBUG_INFO_END **/
794807
}
795808
return result || false
796809
}
@@ -800,19 +813,18 @@ void function (window, _ext) {
800813
}
801814

802815
//fn
803-
function updateSettings() {
804-
_.extend(_.templateSettings, _config)
805-
}
806816
function add(id, templateCode) {
807817
//todo: accept second param as a function, to support pre-compiled template.
808-
if (!id || !_.isString(id)) return false
809-
id = _.str.stripHash(id)
818+
if (arguments.length < 2) return false
819+
810820
var result
811821
if (templateCode) {
812822
var templateId = _toTemplateId(id)
823+
/** DEBUG_INFO_START **/
813824
if (_cacheTemplate[templateId]) {
814-
console.warn('Template cache already has id: "' + templateId + '"')
825+
console.warn('[Template] Template id "' + templateId + '" already existed.')
815826
}
827+
/** DEBUG_INFO_END **/
816828
result = _cacheTemplate[templateId] = templateCode
817829
} else {
818830
//todo: support `_.template.add(id)` to add from dummy script element
@@ -862,14 +874,31 @@ void function (window, _ext) {
862874
return result || ''
863875
}
864876

865-
//init
866-
updateSettings()
867-
877+
/** DEBUG_INFO_START **/
868878
//exports for unit test
879+
template.__toTemplateId = _toTemplateId
880+
template.__toElementId = _toElementId
869881
template.__isTemplateCode = _isTemplateCode
870882
template.__stripCommentTag = _stripCommentTag
871883
template.__cacheTemplate = _cacheTemplate
872884
template.__cacheCompiledTemplate = _cacheCompiledTemplate
885+
/** DEBUG_INFO_END **/
886+
887+
//exports
888+
return template
889+
890+
}()
891+
892+
var _config = {
893+
//compatible with ejs
894+
interpolate : /<%-([\s\S]+?)%>/g,
895+
escape : /<%=([\s\S]+?)%>/g,
896+
897+
//to avoid use `with` in compiled templates
898+
//see: https://github.com/cssmagic/blog/issues/4
899+
variable: 'data'
900+
}
901+
_.extend(_.templateSettings, _config)
873902

874903
//exports
875904
_ext.exports('template', template)

src/adapter-dist-trad/_defense.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
//check dependency
3+
if (!_ || !$) return false
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,3 @@
33
var _ = window._
44
var $ = window.Zepto || window.jQuery || window.$
55
var document = window.document
6-
7-
//check dependency
8-
if (!_ || !$) return false

0 commit comments

Comments
 (0)