javascript - Breeze Setting Multiple Foreign Keys On The Client-side -
i have 3 breeze entities, application long form adds records 3 tables @ once before going server save. 2 of tables drivers & vehicles have relationship autolead , need autoleadid autolead table being entered @ once on client side.
so have form, user entered information autolead, adds records drivers , vehicles tables in 1 seamless form. how id assign drivers , vehicles without first separating saves out, , saving autolead first, returning new id , assigning drivers , vehicles before saving them again. want avoid multiple trips server generate id's navigation properties on vehicles , drivers.
from understood, breeze supposed able fixup these id's using temporary id's client side creates. when go save isnt changing these id's. according to: http://www.breezejs.com/documentation/navigation-properties
if try set navigation property directly objects, undefined error navigation property (see code below). using approach, if try set foreign key directly on id's foreign key error sql because trying push -1 server (the temporary id's). know must doing wrong. here of code:
create new client side entities:
// new entity creation functions var createautolead = function() { return manager.createentity('autolead'); }; var createautoleaddriver = function () { return manager.createentity('autoleaddriver'); }; var createautoleadvehicle = function () { return manager.createentity('autoleadvehicle'); }; // save function loop through drivers , vehicles , set autoleadid's before saving var saveautolead = function (newautolead, autoleaddrivers, autoleadvehicles) { // loop through drivers , set foreign keys (var = 0; < autoleaddrivers().length; i++) { autoleaddrivers()[i].autolead(newautolead); } // loop through drivers , set foreign keys (var i2 = 0; i2 < autoleadvehicles().length; i2++) { autoleadvehicles()[i2].autolead(newautolead); } manager.savechanges() .then(savesucceeded) .fail(savefailed); function savesucceeded() { return logger.log('debug message', 'quote saved successfully', null, system.getmoduleid(datacontext), true, config.growltypes.success); } return logger.log('debug message', 'quote saved successfully', null, system.getmoduleid(datacontext),true, config.growltypes.success); };
i'm glad figured out. wanted point out setting fks have worked well. in testing using northwind:
var em = newem(); // acting vehicle var ord = em.createentity("order"); ord.setproperty("orderdate", new date()); // acting driver var emp2 = em.createentity("employee"); emp2.setproperty("firstname", "test fn2"); emp2.setproperty("lastname", "test ln2"); emp2.setproperty("fullname", "foo2"); // acting autolead var emp1 = em.createentity("employee"); emp1.setproperty("firstname", "test fn1"); emp1.setproperty("lastname", "test ln1"); emp1.setproperty("fullname", "foo1");
i can associate emp1 ord , emp2 follows:
ord.employeeid(emp1.employeeid()); emp2.reportstoemployeeid(emp1.employeeid());
or
ord.employee(emp1); emp2.manager(emp1);
and in both cases fks resolved/updated after save. being said, still recommend set navigation property directly instead of dealing fks.
Comments
Post a Comment