
er.template.parse( '<!-- target: sea -->just test 4 ${name}' );

var testModule2 = new er.Module( {
    config: {
        action: [ 
            {
                path   : '/e',
                action : 'testModule2.action'  
            },
            {
                path   : '/e2',
                action : 'testModule2.action2'  
            }
        ]
    }
} );


var testStateName;
var testStateAddress;
var testModelQueryString;
testModule2.action = new er.Action( {
    view: 'sea',

    STATE_MAP: {
        name: 'sea',
        address: 'hainan'
    },
    
    ontracename: function () {
        testStateName = this.model.get( 'name' );
        testStateAddress = this.model.get( 'address' );
    },

    onchangename: function () {
        this.model.set( 'name', 'erik' );
        this.model.set( 'address', 'haikou' );
        this.refresh();
    },

    onreset: function () {
        this.resetState();
    },

    onresetname: function () {
        this.resetState( 'name' );
    },

    onback: function () {
        this.back();
    },
    
    MODEL_QUERY_MAP: {
        myname: 'name',
        myaddress: 'address'
    },

    ontraceqs: function ( arg ) {
        testModelQueryString = this.getQueryStringByModel( arg );
    },

    onsetaddress: function ( address ) {
        this.model.set( 'address', address );
    }
} );

testModule2.action2 = new er.Action( {
    view: 'sea',

    USE_BACK_LOCATION: 1,
    BACK_LOCATION: '/e',
    onback: function () {
        this.back();
    }
} );

module("er.extend.actionEnhance");

test("状态保持与刷新", function() {
    er.locator.redirect('/e');

    er.controller.fireMain('tracename');
    same( testStateName, "sea", "默认状态值被填充" );
    same( testStateAddress, "hainan", "默认状态值被填充" );

    er.controller.fireMain('changename');
    var loc = er.locator.getLocation();
    same( loc.indexOf("/e~") === 0 && /name=erik/.test(loc) && /address=haikou/.test(loc), true, "刷新时，state被带入location" );
    er.controller.fireMain('tracename');
    same( testStateName, "erik", "状态发生变更" );
    same( testStateAddress, "haikou", "状态发生变更" );

    er.controller.fireMain('reset');
    er.controller.fireMain('tracename');
    same( testStateName, "sea", "状态被恢复" );
    same( testStateAddress, "hainan", "状态被恢复" );

    er.controller.fireMain('changename');
    er.controller.fireMain('resetname');
    er.controller.fireMain('tracename');
    same( testStateName, "sea", "name状态被恢复" );
    same( testStateAddress, "haikou", "address状态保留" );

    er.controller.fireMain('changename');
    er.locator.redirect('/e2');
    er.locator.redirect('/e');
    er.controller.fireMain('tracename');
    same( testStateName, "erik", "状态被保持" );
    same( testStateAddress, "haikou", "状态被保持" );
});

test("back", function() {
    er.locator.redirect('/');
    er.locator.redirect('/e');

    er.controller.fireMain('back');
    same( er.locator.getLocation(), "/", "回退到之前的location" );
    
    er.locator.redirect('/');
    er.locator.redirect('/e2');
    er.controller.fireMain('back');

    same( er.locator.getLocation(), "/e", "配置了USE_BACK_LOCATION时，回退到配置的url" );
});

test("getQueryStringByModel", function() {
    er.locator.redirect('/e');
    er.controller.fireMain('reset');
    er.controller.fireMain('traceqs');
    var str = testModelQueryString;
    same( /myname=sea/.test(str) && /myaddress=hainan/.test(str), true, "自动按映射从model拼接参数" );

    er.controller.fireMain('traceqs', {'testname':'name'});
    same( testModelQueryString, "testname=sea", "自定义映射从model拼接参数" );
    
    var addr = '海口';
    er.controller.fireMain('setaddress',addr);
    er.controller.fireMain('traceqs', {'addr':'address'});
    same( testModelQueryString, "addr=" + encodeURIComponent(addr), "参数自动转义" );
});




@@ -434,16 +434,41 @@
 String;%0A
+var leaved;%0Avar entered;%0A
 testModu
@@ -587,24 +587,80 @@
     %7D,%0A    %0A
+    onenter: function () %7B%0A        entered = 1;%0A    %7D,%0A%0A
     ontracen
@@ -1415,24 +1415,156 @@
  address );%0A
+    %7D,%0A%0A    onleave: function () %7B%0A        leaved = 1;%0A    %7D,%0A%0A    onoverdue: function () %7Bconsole.log(this)%0A        this.reload();%0A
     %7D%0A%7D );%0A%0A
@@ -2893,24 +2893,24 @@
  %22%E7%8A%B6%E6%80%81%E8%A2%AB%E4%BF%9D%E6%8C%81%22 );%0A
-
     same( te
@@ -2947,24 +2947,424 @@
 %E4%BF%9D%E6%8C%81%22 );%0A%7D);%0A%0A
+test(%22reload%22, function() %7B%0A    er.locator.redirect('/');%0A    er.locator.redirect('/e~name=reload');%0A    leaved = 0;%0A    entered = 0;%0A    document.getElementById('Main').innerHTML = '';%0A    er.controller.fireMain( 'overdue' );%0A    same( leaved, 1, %22%E5%BD%93%E5%89%8Daction%E8%A2%AB%E5%8D%B8%E8%BD%BD%E8%BF%87%22 );%0A    same( entered, 1, %22%E5%BD%93%E5%89%8Daction%E8%A2%AB%E9%87%8D%E6%96%B0%E8%A3%85%E8%BD%BD%22 );%0A    same( document.getElementById('Main').innerHTML, 'just test 4 reload', %22%E8%A7%86%E5%9B%BE%E8%A2%AB%E9%87%8D%E6%96%B0%E7%BB%98%E5%88%B6%22 );%0A%7D);%0A%0A
 test(%22back%22,

add reload case