2

I'm building a site as a Single Page Application using ASP.NET MVC 4 Beta .

The sample app talks about adding new entities and it uses a constructor function for it's product entity.

However I have many entity types and I'm not going to write a constructor function for each one. This is how I am creating a new entity (name is the name of the datasource and dataTarget.upshot.upshotData is the list of entities I get back from the GetEntities method in coffeeScript...

newItem = {}
for field, def of upshot.metadata(upshot.dataSources[name]._entityType).fields
    do (field, def) ->
    if def.array
        newItem[field] = new ko.observableArray()
    else
        newItem[field] = new ko.observable()
        upshot.addEntityProperties newItem, upshot.dataSources[name]._entityType
        dataTarget.upshot.upshotData.push newItem 

my question is if this is the best way to do it or am I missing something? I'm surprised that upshot does not seem to have a createEntity method.

in javascript...

newItem = {};
_ref = upshot.metadata(upshot.dataSources[name]._entityType).fields;
_fn = function(field, def) {
    if (def.array) {
       return newItem[field] = new ko.observableArray();
    } else {
       return newItem[field] = new ko.observable();
    }
};
      for (field in _ref) {
        def = _ref[field];
        _fn(field, def);
      }
      upshot.addEntityProperties(newItem, upshot.dataSources[name]._entityType);
      dataTarget.upshot.upshotData.push(newItem);

1 Answers1

2
var newThing = {};
var typeName = "MyType:#MyNamespace";
upshot.map({ SomeProperty: "my value" }, typeName, newThing);
upshot.addEntityProperties(newThing, typeName);

This will create your object with the entity properties mapped to observables, and will allow you to set properties (see SomeProperty:"my value").

Marcus L
  • 463
  • 4
  • 7
  • Hi this does initialise an item with the fields - however the array properties are not set to observable arrays just observables. – Giles Bradshaw Mar 30 '12 at 09:51