Click here to Skip to main content
15,896,557 members
Articles / Web Development / ASP.NET

ASP.NET DaST to wrestle with MVC and WebForms

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
26 Mar 2013CPOL82 min read 23.4K   152   11  
DaST is a new architectural pattern for building highly dynamic Web 2.0 applications. A web page is rendered as a set of randomly nested rectangles where each rectangle is controlled individually and every combination of rectangles can be partially updated via AJAX.
// Name: AspNetDaST.js
// Assembly: AspNetDaST.Resources
//---------------------------------------------------------
// Copyright (c) 2011 Roman Gubarenko. All rights reserved.
//---------------------------------------------------------
// AspNetDaST.js
// ASP.NET DaST Framework.

Type.registerNamespace('DaST');
DaST._Scopes = function DaST$Scopes()
{
  this._initialized = false;
  this._refreshScopeHandlers = new Array();
  this._refreshScopeIDs = null;
}

function DaST$_Scopes$initialize()
{
  if (this._initialized) return;

  var prm = Sys.WebForms.PageRequestManager.getInstance();
  var inst = this;
  
  prm.add_initializeRequest(function(sender, args)
  {
    inst._refreshScopeIDs = null;
  });

  prm.add_pageLoading(function(sender, args)
  {
    var executor = prm._request.get_executor();
    var data = inst._extractUpdatePanels(executor);
    inst._refreshScopeIDs = data.panelsToRefreshNode.content.split(',');
  });

  prm.add_pageLoaded(function(sender, args)
  {
    if (inst._refreshScopeIDs)
    {
      for (var i = 0; i < inst._refreshScopeIDs.length; i++)
      {
        var scopeID = inst._refreshScopeIDs[i];
        var handler = inst._refreshScopeHandlers[scopeID];
        if (handler) handler(args.get_dataItems()[scopeID]);
      }
    }
  });

  this._initialized = true;
}

function DaST$_Scopes$Action(scopePath, actionName, actionArgs)
{
  if (typeof (scopePath) != "string" || typeof (actionName) != "string") throw "Invalid action parameters";
  var prm = Sys.WebForms.PageRequestManager.getInstance();
  if (!Array.contains(prm._asyncPostBackControlIDs, scopePath)) prm._asyncPostBackControlIDs.push(scopePath);
  __doPostBack(scopePath, "" + actionName + "|" + JSON.stringify(actionArgs));
}

function DaST$_Scopes$SetRefreshHandler(scopeID, callback)
{
  if (typeof (scopeID) == "string" && typeof (callback) == "function")
  {
    this._refreshScopeHandlers[scopeID] = callback;
  }
}

function DaST$_Scopes$_extractUpdatePanels(executor)
{
  var reply = executor.get_responseData();
  var delimiterIndex, len, type, id, content;
  var replyIndex = 0;
  var delta = [];
  while (replyIndex < reply.length)
  {
    delimiterIndex = reply.indexOf('|', replyIndex);
    if (delimiterIndex === -1) throw "UpdatePanel response parser error";   
    len = parseInt(reply.substring(replyIndex, delimiterIndex), 10);
    if ((len % 1) !== 0) throw "UpdatePanel response parser error";
    replyIndex = delimiterIndex + 1;
    delimiterIndex = reply.indexOf('|', replyIndex);
    if (delimiterIndex === -1) throw "UpdatePanel response parser error";
    type = reply.substring(replyIndex, delimiterIndex);
    replyIndex = delimiterIndex + 1;
    delimiterIndex = reply.indexOf('|', replyIndex);
    if (delimiterIndex === -1) throw "UpdatePanel response parser error";
    id = reply.substring(replyIndex, delimiterIndex);
    replyIndex = delimiterIndex + 1;
    if ((replyIndex + len) >= reply.length) throw "UpdatePanel response parser error";
    content = reply.substr(replyIndex, len);
    replyIndex += len;
    if (reply.charAt(replyIndex) !== '|') throw "UpdatePanel response parser error";
    replyIndex++;
    Array.add(delta, { type: type, id: id, content: content });
  }

  var updatePanelIDsNode = null, panelsToRefreshNode = null;
  for (var i = 0, l = delta.length; i < l; i++)
  {
    var deltaNode = delta[i];
    switch (deltaNode.type)
    {
      case "panelsToRefreshIDs":
        if (!panelsToRefreshNode) panelsToRefreshNode = deltaNode;
        break;
      default: break;
    }
  }

  return { panelsToRefreshNode: panelsToRefreshNode };
}

DaST._Scopes.prototype =
{
  Action: DaST$_Scopes$Action,
  SetRefreshHandler: DaST$_Scopes$SetRefreshHandler,
//  EncodeClientData: DaST$_Scopes$EncodeClientData,

  initialize: DaST$_Scopes$initialize,

  _extractUpdatePanels: DaST$_Scopes$_extractUpdatePanels
}

DaST._Scopes.registerClass('DaST._Scopes');
DaST.Scopes = new DaST._Scopes();


var JSON = JSON || {};
// implement JSON.stringify serialization  
JSON.stringify = JSON.stringify || function(obj)
{
  var t = typeof (obj);
  if (t != "object" || obj === null)
  {
    // simple data type  
    if (t == "string") obj = '"' + obj + '"';
    return String(obj);
  }
  else
  {
    // recurse array or object  
    var n, v, json = [], arr = (obj && obj.constructor == Array);
    for (n in obj)
    {
      v = obj[n]; t = typeof (v);
      if (t == "string") v = '"' + v + '"';
      else if (t == "object" && v !== null) v = JSON.stringify(v);
      json.push((arr ? "" : '"' + n + '":') + String(v));
    }
    return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
  }
};  

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
Software Architect with over 15 years in IT field. Started with deep math and C++ Computer Vision software. Currently in .NET and PHP web development. Creator of DaST pattern, open-source frameworks, and plugins. Interested in cutting Edge IT, open-source, Web 2.0, .NET, MVC, C++, Java, jQuery, Mobile tech, and extreme sports.

Comments and Discussions