3 changed files with 165 additions and 146 deletions
@ -1,70 +1,82 @@ |
|||||
;(function(window) { |
(function(window) { |
||||
var _instance = null; |
var _instance = null; |
||||
window.TallPlugin = function(config) { |
window.TallPlugin = function(config) { |
||||
this.config = config; |
this.config = config; |
||||
this.props = null; |
this.props = null; |
||||
} |
}; |
||||
|
|
||||
// 初始化并保证是单例
|
|
||||
TallPlugin.init = function (config) { |
|
||||
if (!_instance) { |
|
||||
_instance = new TallPlugin(config); |
|
||||
} |
|
||||
return _instance; |
|
||||
} |
|
||||
|
|
||||
TallPlugin.prototype.created = function (callback) { |
// 初始化并保证是单例
|
||||
var _this = this; |
TallPlugin.init = function(config) { |
||||
window.postMessage('created'); |
if (!_instance) { |
||||
window.addEventListener('message', function ({ data, origin }) { |
_instance = new TallPlugin(config); |
||||
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; |
|
||||
} |
} |
||||
|
return _instance; |
||||
|
}; |
||||
|
|
||||
TallPlugin.prototype.mounted = function(callback) { |
function postMsg(message) { |
||||
console.log('mounted'); |
let origin = '*'; |
||||
window.postMessage('mounted'); |
window.postMessage(message, origin); |
||||
callback && typeof callback === 'function' && callback.call(this); |
} |
||||
return this; |
|
||||
} |
|
||||
|
|
||||
TallPlugin.prototype.loaded = function(callback) { |
TallPlugin.prototype.created = function(callback) { |
||||
console.log('loaded'); |
console.log('created begin'); |
||||
window.postMessage('loaded'); |
var _this = this; |
||||
callback && typeof callback === 'function' && callback.call(this); |
postMsg('created'); |
||||
return this; |
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) { |
TallPlugin.prototype.mounted = function(callback) { |
||||
console.log('destroy'); |
console.log('mounted'); |
||||
window.postMessage('destroy'); |
postMsg('mounted'); |
||||
callback && typeof callback === 'function' && callback.call(this); |
callback && typeof callback === 'function' && callback.call(this); |
||||
return this; |
return this; |
||||
} |
}; |
||||
|
|
||||
TallPlugin.prototype.error = function(callback) { |
TallPlugin.prototype.loaded = function(callback) { |
||||
console.log('error'); |
console.log('loaded'); |
||||
window.postMessage('error'); |
postMsg('loaded'); |
||||
callback && typeof callback === 'function' && callback.call(this); |
callback && typeof callback === 'function' && callback.call(this); |
||||
return this; |
return this; |
||||
} |
}; |
||||
})(window) |
|
||||
|
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