Saturday, 1 June 2013

Adding new objects to a clientside array - best practices?

Adding new objects to a clientside array - best practices?

Say you have a list of "vehicles" - you have an observableArray of these ko.observable()s.
You let your user add a new vehicle with the following:
var emptyVehicle = {
    "make": "chevrolet",
    "model": "corvette"
};
app.on('CLICKED_ADD_VEHICLE', function () {
            var vehicle = new vehicleViewModel(emptyVehicle);
            vehicle.hasWheels(true);
            innerModel.sets.push(set);
        });
If they save the vehicle, you get a real vehicle from your dataservice, and then replace the blank one with this:
app.on('CLICKED_SAVE', function (oldVehicle) {
            var newVehicle = new vehicleViewModel(dataservice.createVehicle(oldVehicle));
                    innerModel.vehicles.remove(oldVehicle);
                    innerModel.vehicles.push(newVehicle);
        });
I know I'm leaving some code out here, but this is more of a practices/approach question than a code question. Is this an acceptable approach to adding new items to a list? New up a temporary template, and throw it away in place of an actual item if the user saves?

No comments:

Post a Comment