如何使用BreezeJS查询记录ID

时间:2015-04-20 21:52:06

标签: breeze

我创建了一个带有微风的新条目,在提交时我希望立即使用我用来创建记录的基本字段来获取记录的ID,如使用电子邮件,请问我如何获得新记录的ID微风。

这就是我所做的

bind gotoStep2() with the save button which pass in the value of the textbox to use as predicate, 
// the save() method successfully create the record to the database, 

function gotoStep2(firstName, lastName, email) {
        save();
        console.log('Save log')
        // Get the ProfileID
        return datacontext.profile.getProfileId(firstName, lastName, email)
            .then(function (data) {
                console.log('Id retrived is: ' + data.Id); // check the value returned
                vm.profile = data; 
                //$location.path('/step-two/' + data.Id);
                // Todo: pass the value to the next route
            }, function (error) {
                logError('Unable to get speaker');
            });
    }


function getProfileId(firstName, lastName, email) {
        var self = this;
        var predicate = Predicate.create('firstName', '==', firstName)
                        .and('lastName', '==', lastName)
                        .and('email', '==', email);
        var profiles = [];

        return EntityQuery.from('Profiles')
            .select('id')
            .where(predicate)
            .toType(entityName)
            .using(self.manager).execute()
            .then(querySucceeded, self._queryFailed);

        function querySucceeded(data) {
            profiles = data.results;
            self.log('Retrieved [Profile by email] from remote data source', profiles.length, true);
            return profiles;
        }
    }
从上面的查询

总是得到任何返回值

由于

1 个答案:

答案 0 :(得分:0)

我不完全确定我理解您的问题,但听起来您希望在保存后立即获取新保存记录的ID。如果是,则以下答案适用。

当保存承诺解析时,它会返回已保存实体的列表以及 keyMappings 数组,以用于因保存而更改了ID的任何实体。即从临时到真实ID的映射。即(在此记录:http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html#method_saveChanges

myEntityManager.saveChanges().then(function (saveResult) {
  // entities is an array of entities that were just saved.
  var entitites = saveResult.entities;
  var keyMappings = saveResult.keyMappings;
  keyMappings.forEach(function(km) {
    var tempId = km.tempValue;
    var newId = km.realValue;
  });
});

另一方面,如果您有一个实体而您只想要它的“密钥”,则可以使用 EntityAspect.getKey 方法。 (见http://www.breezejs.com/sites/all/apidocs/classes/EntityAspect.html#method_getKey

// assume order is an order entity attached to an EntityManager.
var entityKey = order.entityAspect.getKey();