3 changed files with 165 additions and 146 deletions
@ -1,70 +1,82 @@ |
|||
;(function(window) { |
|||
var _instance = null; |
|||
window.TallPlugin = function(config) { |
|||
this.config = config; |
|||
this.props = null; |
|||
} |
|||
|
|||
// 初始化并保证是单例
|
|||
TallPlugin.init = function (config) { |
|||
if (!_instance) { |
|||
_instance = new TallPlugin(config); |
|||
} |
|||
return _instance; |
|||
} |
|||
(function(window) { |
|||
var _instance = null; |
|||
window.TallPlugin = function(config) { |
|||
this.config = config; |
|||
this.props = null; |
|||
}; |
|||
|
|||
TallPlugin.prototype.created = function (callback) { |
|||
var _this = this; |
|||
window.postMessage('created'); |
|||
window.addEventListener('message', function ({ data, origin }) { |
|||
try { |
|||
var target = JSON.parse(data); |
|||
if (target.success) { |
|||
_this.props = JSON.parse(data); |
|||
callback && typeof callback === 'function' && callback.call(_this, _this.props); |
|||
} else { |
|||
_this.props = null; |
|||
} |
|||
} catch (e) { |
|||
_this.props = null; |
|||
} |
|||
}, false); |
|||
// DOM加载完成
|
|||
window.addEventListener('DOMContentLoaded', this.mounted, false); |
|||
// window onload
|
|||
window.addEventListener('load', this.loaded, false); |
|||
// destroy
|
|||
window.addEventListener('unload', this.destroy, false); |
|||
// error
|
|||
window.addEventListener('error', this.error, false); |
|||
return this; |
|||
// 初始化并保证是单例
|
|||
TallPlugin.init = function(config) { |
|||
if (!_instance) { |
|||
_instance = new TallPlugin(config); |
|||
} |
|||
return _instance; |
|||
}; |
|||
|
|||
TallPlugin.prototype.mounted = function(callback) { |
|||
console.log('mounted'); |
|||
window.postMessage('mounted'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
} |
|||
function postMsg(message) { |
|||
let origin = '*'; |
|||
window.postMessage(message, origin); |
|||
} |
|||
|
|||
TallPlugin.prototype.loaded = function(callback) { |
|||
console.log('loaded'); |
|||
window.postMessage('loaded'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
} |
|||
TallPlugin.prototype.created = function(callback) { |
|||
console.log('created begin'); |
|||
var _this = this; |
|||
postMsg('created'); |
|||
window.addEventListener( |
|||
'message', |
|||
function({ data, origin }) { |
|||
console.log('on created message, data, origin: ', data, origin); |
|||
try { |
|||
var target = JSON.parse(data); |
|||
if (target.success) { |
|||
_this.props = JSON.parse(data); |
|||
callback && typeof callback === 'function' && callback.call(_this, _this.props); |
|||
} |
|||
// else {
|
|||
// _this.props = null;
|
|||
// }
|
|||
} catch (e) { |
|||
_this.props = null; |
|||
} |
|||
}, |
|||
false, |
|||
); |
|||
// DOM加载完成
|
|||
window.addEventListener('DOMContentLoaded', this.mounted, false); |
|||
// window onload
|
|||
window.addEventListener('load', this.loaded, false); |
|||
// destroy
|
|||
window.addEventListener('unload', this.destroy, false); |
|||
// error
|
|||
window.addEventListener('error', this.error, false); |
|||
return this; |
|||
}; |
|||
|
|||
TallPlugin.prototype.destroy = function(callback) { |
|||
console.log('destroy'); |
|||
window.postMessage('destroy'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
} |
|||
TallPlugin.prototype.mounted = function(callback) { |
|||
console.log('mounted'); |
|||
postMsg('mounted'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
}; |
|||
|
|||
TallPlugin.prototype.error = function(callback) { |
|||
console.log('error'); |
|||
window.postMessage('error'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
} |
|||
})(window) |
|||
TallPlugin.prototype.loaded = function(callback) { |
|||
console.log('loaded'); |
|||
postMsg('loaded'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
}; |
|||
|
|||
TallPlugin.prototype.destroy = function(callback) { |
|||
console.log('destroy'); |
|||
postMsg('destroy'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
}; |
|||
|
|||
TallPlugin.prototype.error = function(callback) { |
|||
console.log('error'); |
|||
postMsg('error'); |
|||
callback && typeof callback === 'function' && callback.call(this); |
|||
return this; |
|||
}; |
|||
})(window); |
|||
|
Loading…
Reference in new issue