Click here to Skip to main content
15,885,890 members
Articles / Web Development / HTML5

Property Finder - a Cross-Platform HTML5 Mobile App

Rate me:
Please Sign up or sign in to vote.
5.00/5 (45 votes)
3 Sep 2012CPOL27 min read 133.8K   2.4K   94  
This article describes the development of a cross-platform HTML5 application for Windows Phone and iPhone.
/// <reference path="..//intellisense.js" />

/*global ko, $, window */

function hydrateObject(state) {
  /// <summary>
  /// Takes a JSON representation of view model state and creates view model instances
  /// via their constructor function as indicated by the 'factoryName' property.
  /// </summary>

  // if state is a primitive type, rather than an object - no 
  // need to hydrate;
  if (!(state instanceof Object)) {
    return state;
  }

  var property, unwrapped, propertyValue,
  // create the required view model instance
    viewModel = new window["ViewModel"][state.factoryName]();

  // iterate over each state property
  for (property in state) {
    if (property === "template" ||
        property === "factoryName" ||
        property === undefined) {
      continue;
    }

    propertyValue = state[property];

    // if the view model property is a function - it might be a KO observable
    if (viewModel[property] instanceof Function) {

      // check if this is a KO observable
      unwrapped = ko.utils.unwrapObservable(viewModel[property]);

      // if after unwrapping we do not get the same object back, then
      // this is an observable
      if (viewModel[property] !== unwrapped) {

        // check if this is an array observable
        if (unwrapped instanceof Array) {
          $.each(propertyValue, function () {
            viewModel[property].push(hydrateObject(this));
          });
        } else {
          // otherwise set the value via the observable setter
          viewModel[property](propertyValue);
        }
      }

    } else {
      viewModel[property] = hydrateObject(propertyValue);
    }
  }

  return viewModel;
}

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
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions