I guess I figured it out myself :)
function GetD(tb)
{
$this = document.getElementById(tb);
var obj = new MyData($this);
return obj;
}
function MyData(tb)
{
this.init(tb);
}
MyData.prototype = {
init : function (tb){
this.div = tb;
this.refresh();
},
refresh: function(){
var d = this;
$.ajax({
type: 'POST',
url: 'Default.aspx/Fetch',
data: "{}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(result) {d.UpdateData(result);}
});
},
UpdateData: function (result) {
var obj = this.div;
obj.innerHTML = result.d;
}
}
Thanks
George
On Nov 9, 11:58 pm, George <[EMAIL PROTECTED]> wrote:
> I am writing an object that gets data using AJAX and shows it in <DIV>
> Also this object must provide refresh method so data can be easily
> refreshed.
>
> Here is my implementation
>
> function GetD(tb)
> {
> $this = document.getElementById(tb);
> var obj = new MyData($this);
> return obj;
>
> }
>
> function MyData(tb)
> {
> this.init(tb);
>
> }
>
> MyData.prototype = {
> init : function (tb){
> this.div = tb;
> this.refresh();
> },
>
> refresh: function(){
> var tb = this.div;
> $.ajax({
> type: 'POST',
> url: 'Default.aspx/Fetch',
> data: "{}",
> contentType: 'application/json; charset=utf-8',
> dataType: 'json',
> success: function(result) {Populate( tb, result);}
> });
> }
>
> }
>
> function Populate (tb, result)
> {
> tb.innerHTML = result.d;
>
> }
>
> and the actual use:
>
> <div id="test"></div>
>
> <script>
> var o = GetD('test');
> ...blablalba.....
> o.refresh();
> </script>
>
> Question: Is is possible to move Populate into MyData object?
>
> PS: I realize that the whole thing does not make much sense but since
> I have trouble fully understand how 'this' and objects work in
> JavaScript I am doing this as an exercise.
>
> Thanks
> George.