2012-08-10 17 views
14

जिज्ञासा से बाहर और मेरा ज्ञान बढ़ाने के लिए, मैं डोम तत्वों और जावास्क्रिप्ट चर के बीच बाध्यकारी के दो तरह के डेटा को लागू करना चाहता था।सादा जावास्क्रिप्ट बिडरेक्शनल डेटा बाध्यकारी

मैं भाग्यशाली था कि मेरी समस्या का आधा हिस्सा यहां @ स्टैक ओवरफ्लो जो मुझे इस जिस्ट https://gist.github.com/384583 पर ले गया, लेकिन मुझे अभी भी 100% पर काम नहीं मिल रहा है।

यहाँ मेरी कोड के उदाहरण है: http://jsfiddle.net/bpH6Z/

आप बेला चलाने के लिए और पर "दृश्य मान" क्लिक करने के लिए प्रयास करते हैं तो आप अपरिभाषित मिल जाएगा, जबकि मैं के वास्तविक मूल्य प्राप्त करना चाहते हैं ऑब्जेक्ट की विशेषता।

मैं जावास्क्रिप्ट के साथ अनुभव की कमी के कारण शायद कुछ गलत कर रहा हूं, लेकिन क्या आपको कोई विचार है कि मैं _bind() और _watch() कॉल के बाद विशेषता 'गुप्त' को सही ढंग से क्यों नहीं पढ़ सकता?

अस्वीकरण: जैसा कि मैंने कहा, मैं ऐसा इसलिए कर रहा हूं क्योंकि मुझे जावास्क्रिप्ट का बेहतर ज्ञान चाहिए, और मैं अपना ढांचा लिखने वाला नहीं हूं। तो कोई भी "फ्रेम फ्रेम एक्स" पूरी तरह से बेकार है, क्योंकि मैं कोणीय के साथ काम कर सकता था।

+0

+1 सादा जे एस के लिए;) – metadings

+0

IST $ jQuery की चर्चा करते हुए? तो यह सादा जेएस हुह – daslicht

+0

संभावित डुप्लिकेट [जावास्क्रिप्ट में डोम डेटा बाइंडिंग को कार्यान्वित करने के लिए कैसे करें] (http://stackoverflow.com/questions/16483560/how-to-implement-dom-data-binding-in-javascript) – Beginner

उत्तर

5

कोशिश http://jsfiddle.net/bpH6Z/4/

मैं Object.prototype .__ घड़ी में अपने नए सिरे से परिभाषित गेटर/सेटर को नवीनीकृत किया है, भी वर्तमान में आपके हैंडलर नई मूल्य वापस करने की जरूरत है कृपया।

अपडेट: अब आपके हैंडलर को नए सेट वैल्यू को वापस करने की आवश्यकता नहीं है।

वर्तमान कोड:

//Got this great piece of code from https://gist.github.com/384583 
Object.defineProperty(Object.prototype, "__watch", { 
    enumerable: false, 
    configurable: true, 
    writable: false, 
    value: function(prop, handler) { 
     var val = this[prop], 
      getter = function() { 
       return val; 
      }, 
      setter = function(newval) { 
       val = newval; 
       handler.call(this, prop, newval); 
       return newval; 
      }; 

     if (delete this[prop]) { // can't watch constants 
      Object.defineProperty(this, prop, { 
       get: getter, 
       set: setter, 
       enumerable: true, 
       configurable: true 
      }); 
     } 
    } 
}); 

var Controller = function() { 
    //The property is changed whenever the dom element changes value 
    //TODO add a callback ? 
    this._bind = function (DOMelement, propertyName) { 
     //The next line is commented because priority is given to the model 
     //this[propertyName] = $(DOMelement).val(); 
     var _ctrl = this; 
     $(DOMelement).on("change input propertyChange", function(e) { 
      e.preventDefault(); 
      _ctrl[propertyName] = DOMelement.val(); 
     }); 

    }; 

    //The dom element changes values when the propertyName is setted 
    this._watch = function(DOMelement, propertyName) { 
     //__watch triggers when the property changes 
     this.__watch(propertyName, function(property, value) { 
      $(DOMelement).val(value); 
     }); 
    }; 
}; 

var ctrl = new Controller(); 
ctrl.secret = 'null'; 
ctrl._bind($('#text1'), 'secret'); // I want the model to reflect changes in #text1 
ctrl._watch($('#text2'), 'secret'); // I want the dom element #text2 to reflect changes in the model 
$('#button1').click(function() { 
    $('#output').html('Secret is : ' + ctrl.secret); //This gives problems 
}); 

वर्तमान HTML:

<html> 
<head></head> 
<body> 
    value: <input type="text" id="text1" /><br /> 
    copy: <input type="text" id="text2" /><br /> 
    <input type="button" id="button1" value="View value"><br /> 
    <span id="output"></span> 
</body> 
</html> 
+0

+1 को हटाने के लिए देखा है। क्या यह IE8 के साथ काम करेगा परिभाषाप्रॉपर्टी केवल डीओएम ऑब्जेक्ट्स पर काम करता है? – fatmatto

+0

के लिए अपने सिरदर्द – Integralist

+0

@ इंटेग्रलिस्ट सबसे अधिक संभावना नहीं है, लेकिन किसी को इसे आज़माएं - एक्सपी पर रीबूट करना होगा – metadings

3

आपके __watch फ़ंक्शन में दिए गए हैंडलर में return कथन अनुपलब्ध है। newval क्या हैंडलर से लौट आए है पर सेट है

this._watch = function(DOMelement, propertyName) { 
    //__watch triggers when the property changes 
    this.__watch(propertyName, function(property, value) { 
     $(DOMelement).val(value); 
     return value; 
    }) 

} 

वजह से, यह undefined कि बिना हो जाएगा।

+0

+1। मुझे स्पॉट करें, मुझे इसे हराएं ... मैंने अभी भी –

+0

+1 को रीडवर्क – fatmatto

0

मैं बहु पर नजर रखने वालों के लिए यह वृद्धि की है।

http://jsfiddle.net/liyuankui/54vE4/

//The dom element changes values when the propertyName is setted 
this._watch = function(DOMelement, propertyName) { 
    //__watch triggers when the property changes 
    if(watchList.indexOf(DOMelement)<0){ 
     watchList.push(DOMelement); 
    } 
    this.__watch(propertyName, function(property, value) { 
     for(var i=0;i<watchList.length;i++){ 
      var watch=watchList[i]; 
      $(watch).val(value); 
      $(watch).html(value); 
     } 
    }); 
}; 
संबंधित मुद्दे