2012-10-09 95 views
2

我想創建一個對象,其第一個屬性是一個函數,用於確定父對象訪問哪個子對象。我的具體應用是根據提交的表單類型來改變表單驗證的類型。將函數分配給JSON屬性

錯誤消息我得到在頁面加載我是:Uncaught TypeError: Cannot set property 'validate' of undefined

這裏是我的JavaScript至今(這裏的的jsfiddle:http://jsfiddle.net/WJRwv/3/):

var O={ 
    form:{ 
    voting:{ 
     validate: {}, success: {} 
    }, 
    delete:{ 
     validate: {}, success: {} 
    } 
    } 
}; 

O.form=function(formType){ 
    if(formType=='voting'){ 
    return O.form.voting; 
    } 
    else if(formType=='delete'){ 
    return O.form.delete; 
    } 
} 

THE LINE正下方導致錯誤

O.form.voting.validate=function($form){ //**THIS LINE IS CAUSING THE ERROR** 
    if($form.validate().form()){ // this is a method from the jQuery validate plugin 
    console.log('YES validates'); 
    } 
    else { 
    console.log('NOT valid'); return false; 
    } 
} 

$('input[type=submit]').click(function(){ 
    var formType=$(this).data('form-type'), 
     $form=$(this).closest('form'), 
     formType=O.form(formType); 
     console.log('form type is:'+formType); 
     formType($form); //this should call the O.form.voting.validate method 
}); 

HTML:

<form> 
    <input type="submit" data-form-type='voting' name='voting' value="1"> 
</form> 

<form> 
    <input type="submit" data-form-type='delete' name='delete' value="1"> 
</form> 

回答

1

您的O.form對象文字被O.form函數覆蓋。相反...

var O = { 
    form: function(formType) { 
    if(formType=='voting'){ 
     return O.form.voting; 
    } 
     else if(formType=='delete'){ 
     return O.form.delete; 
    } 
    } 
}; 

O.form.voting = { 
    validate: {}, success: {} 
}; 
O.form.delete = { 
    validate: {}, success: {} 
}; 
4

問題是您之前使用函數覆蓋了o.form,因此o.form.voting(以及您在其中設置的其他內容)不再存在。

+0

- @ prodigitalson ,謝謝我意識到,但是我在哪裏定義'o.form',以便我可以切換使用'o.form.voting'或'o.form.delete'的位置? –

+0

這取決於你希望它的功能,比如你希望以後公開和重寫什麼,除非你實際編輯代碼,你想要「保護」什麼? – prodigitalson

1

這是造成問題的原因:

O.form=function 

現在form不再有voting屬性。

+0

- @ cammil,謝謝我意識到,但是我在哪裏定義了'o.form',以便我可以切換使用'o.form.voting'或'o.form.delete'的位置? –

0

你可以做的反而是創建一個映射表單驗證設置的功能,jQuery的驗證允許每個形式的驗證,並制定出提交表單時要使用的驗證......

更新HTML ...

<form data-form-type="voting"> 
    <input type="submit" name="voting" value="1"/> 
</form> 

<form data-form-type="delete"> 
    <input type="submit" name="delete" value="1"/> 
</form> 

更新JS ...

var o = {}; 
o.setFormValidations = function(types) { 
    // Loop through forms and attempt to map each form to an item in the passed 
    // types argument 
    $("form").each(function() { 
     // Get the form type from the data attribute 
     var type = $(this).data("form-type"); 
     // Add in the validator if there is a matching item in the passed 
     // types arguments 
     if(types[type]) { 
      $(this).validate(types[type]); 
     } 
    }); 
}; 

// On document ready we run the function passing in the jQuery validator 
// settings for each form type 
$(function() { 
    namespace.setFormValidations({ 
     "voting" : { 
      rules: {}, 
      messages: {}, 
      submitHandler: function() { 
       // Do something for a valid form 
      } 
     }, 
     "delete" : { 
      rules: {}, 
      messages: {}, 
      submitHandler: function() { 
       // Do something for a valid form 
      } 
     } 
    }); 
});