246 lines
4.7 KiB
JavaScript
246 lines
4.7 KiB
JavaScript
|
|
/*******************************************************************************
|
|
*
|
|
* Nx单视图类
|
|
*
|
|
*
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
const NxObject = require( "nx.object" );
|
|
|
|
var NxView = cc.Class( {
|
|
|
|
extends: NxObject,
|
|
|
|
name: "NxView",
|
|
|
|
// 初始化
|
|
initialize: function( _args ) {
|
|
|
|
if( !this._super( _args ) ) {
|
|
return false;
|
|
}
|
|
|
|
this.vid = "";
|
|
this.model = null;
|
|
|
|
this.flag = "";
|
|
this.props = {};
|
|
|
|
return true;
|
|
},
|
|
|
|
// 销毁
|
|
uninitialize: function() {
|
|
|
|
this.vclear();
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
|
|
// 标签设置
|
|
vflag: function( flag ) {
|
|
this.flag = flag;
|
|
},
|
|
|
|
// 重置
|
|
reset: function( _model ) {
|
|
|
|
_model = _model ? _model : this.model;
|
|
if( !nx.dt.arrGood( _model ) ) {
|
|
nx.error( "[单视图]重置失败,错误的模板!" );
|
|
return false;
|
|
}
|
|
|
|
this.model = nx.dt.objClone( _model );
|
|
|
|
for( let i = 0; i < this.model.length; ++i ) {
|
|
|
|
var p = this.model[ i ];
|
|
if( !nx.dt.objGood( p ) || nx.dt.strEmpty( p.key ) ) {
|
|
nx.error( "[单视图]重置出错,模板中有错误的设定! 第%d个属性", i );
|
|
continue;
|
|
}
|
|
|
|
this.vset( p.key, p.value, true, p.desc );
|
|
}
|
|
|
|
return true;
|
|
},
|
|
|
|
// 设置
|
|
vset: function( key, value, notify, desc ) {
|
|
|
|
if( nx.dt.strEmpty( key ) ) {
|
|
nx.error( "[单视图]属性查询失败,无效的属性名!" );
|
|
return false;
|
|
}
|
|
|
|
// nx.debug( "props:[" + this.flag + "] " + key + " -> " + value )
|
|
|
|
// 更新
|
|
var p = this.props[ key ];
|
|
if( p != null ) {
|
|
p.old = nx.dt.objClone( p.now );
|
|
p.now = nx.dt.objClone( value );
|
|
}
|
|
else {
|
|
this.props[ key ] = { now: nx.dt.objClone( value ), old: nx.dt.objClone( value ), binders: [], desc: desc };
|
|
}
|
|
|
|
// 通告
|
|
if( notify != false ) {
|
|
this._notify( this.props[ key ] );
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
// 广播一次
|
|
vnotify: function( key ) {
|
|
const p = this.props[ key ];
|
|
if( p != null ) {
|
|
this._notify( p );
|
|
}
|
|
else {
|
|
nx.error( "[单视图]未定义的属性: " + key );
|
|
}
|
|
},
|
|
|
|
// 获取
|
|
vget: function( key, def ) {
|
|
var p = this._query( key );
|
|
return p ? nx.dt.objClone( p.now ) : def;
|
|
},
|
|
|
|
// 获取老值
|
|
vgetOld: function( key, def ) {
|
|
var p = this._query( key );
|
|
return p ? nx.dt.objClone( p.old ) : def;
|
|
},
|
|
|
|
// 监听
|
|
vbind: function( key, callback, tag ) {
|
|
|
|
if( typeof ( tag ) != "string" || tag == "" ) {
|
|
nx.error( "[单视图]监听失败,标记必须是有效字符串" );
|
|
return false;
|
|
}
|
|
|
|
var p = this._query( key );
|
|
if( p == null ||
|
|
typeof ( callback ) != "function" ) {
|
|
nx.error( "[单视图]属性监听失败,参数不匹配!" );
|
|
return false;
|
|
}
|
|
|
|
for( var i = 0; i < p.binders.length; i++ ) {
|
|
var cb = p.binders[ i ];
|
|
if( cb == callback ) {
|
|
nx.error( "[单视图]重复监听:" + key );
|
|
return false;
|
|
}
|
|
}
|
|
|
|
p.binders.push( { cb: callback, tag: tag } );
|
|
|
|
nx.dt.trycatch( () => {
|
|
callback( p.now, p.old, true );
|
|
} );
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
// 批量监听
|
|
vbinds: function( tag, _lst ) {
|
|
|
|
if( typeof ( tag ) != "string" || tag == "" ) {
|
|
nx.error( "[单视图]批量监听失败,标记必须是有效字符串" );
|
|
return false;
|
|
}
|
|
|
|
if( _lst.length == 0 ) {
|
|
nx.error( "[单视图]批量监听失败,监听列表空!" + tag );
|
|
return false;
|
|
}
|
|
|
|
for( var i in _lst ) {
|
|
var binder = _lst[ i ];
|
|
this.vbind( binder[ 0 ], binder[ 1 ], tag );
|
|
}
|
|
|
|
// nx.debug( "为[%s]增加监听[%d]个.", tag, _lst.length );
|
|
return true;
|
|
|
|
},
|
|
|
|
// 监听解除
|
|
vunbind: function( tag ) {
|
|
|
|
if( typeof ( tag ) != "string" || tag == "" ) {
|
|
nx.error( "[单视图]监听解除失败,标记必须是有效字符串" );
|
|
return false;
|
|
}
|
|
|
|
var count = 0;
|
|
for( var k in this.props ) {
|
|
var p = this.props[ k ];
|
|
for( var i = p.binders.length - 1; i >= 0; i-- ) {
|
|
var binder = p.binders[ i ];
|
|
if( binder && binder.tag == tag ) {
|
|
p.binders.splice( i, 1 );
|
|
count++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// nx.debug( "为[%s]解除监听[%d]个.", tag, count );
|
|
return true;
|
|
|
|
},
|
|
|
|
// 清理
|
|
vclear: function() {
|
|
this.props = {};
|
|
},
|
|
|
|
// 查询
|
|
_query: function( _key ) {
|
|
|
|
if( typeof ( _key ) != "string" || _key == "" ) {
|
|
nx.error( "[单视图]属性查询失败,无效的属性名!" );
|
|
return null;
|
|
}
|
|
|
|
var p = this.props[ _key ];
|
|
if( typeof ( p ) == "undefined" ) {
|
|
nx.error( "[单视图]属性查询失败,不存在属性:" + _key );
|
|
return null;
|
|
}
|
|
|
|
return p;
|
|
},
|
|
|
|
// 通告
|
|
_notify: function( _prop ) {
|
|
|
|
nx.dt.trycatch( () => {
|
|
for( var i in _prop.binders ) {
|
|
var binder = _prop.binders[ i ];
|
|
if( binder && typeof ( binder.cb ) == "function" ) {
|
|
binder.cb( _prop.now, _prop.old, false );
|
|
}
|
|
}
|
|
} );
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
module.exports = NxView;
|