Jquery Mobile开发以及Js对象动态绑定

时间:2023-01-03 09:15:29

动态创建对象并绑定属性:

            var instantiate = function (Type, args) {
var Constructor = function () { };
Constructor.prototype = Type.prototype; var obj = new Constructor(); Type.apply(obj, args); return obj;
}; var AssetObj = function () { }; AssetObj.prototype.getAssetName = function (AssetName) {
return "AssetName:" + this[AssetName] + ",data:" + this[AssetName + "DataObjs"] + "Length:" + this[AssetName + "DataObjs"].length;
}; AssetObj.prototype.set = function (Asset, data) {
this[Asset] = Asset;
if (this[Asset + "DataObjs"] == null || this[Asset + "DataObjs"] == undefined) {
this[Asset + "DataObjs"] = new Array();
this[Asset + "DataObjs"].push(data);
} else {
this[Asset + "DataObjs"].push(data);
}
} //
var assetObjs = instantiate(AssetObj);
var regionObjs = instantiate(AssetObj); //assetObjs.set("ISCS", "ISCS_DATA1");
//assetObjs.set("ISCS", "ISCS_DATA2");
//assetObjs.set("HHO", "HHO_DATA");
//alert("person2:" + assetObjs.getAssetName('ISCS'));
//alert("person3:" + assetObjs.getAssetName('HHO')); $.each(items, function (k, v) {
assetObjs.set(v.Asset, v);
regionObjs.set(v.Region, v);
});

Jquery属性选择器:

源码示例:

//获取页面data-role="page"和data-role="dialog"的数目
//#7470
var $pages = $(":jqmData(role='page'),:jqmData(role='dialog')")

那我来大家来看一下到底是咋样实现的呢?

源码展示:

var oldFind = $.find,
jqmDataRE = /:jqmData\(([^)]*)\)/g; $.find = function(selector,context,ret,extra){
//注意这边会按照jquery mobile的特性写法,进行一层过滤
selector = selector.replace(jqmDataRE,"[data-" + ($.mobile.ns || "") + "$1]");
//比如selector为":jqmData(role='page')",替换后的"[data-role='page']"
return oldFind.call(this,selector,context,ret,extra);
};

说明;
1、其实在jquery mobile体系中,内部处理了一下$.find,对:jqmData这种选择器进行处理