【问题标题】:How to Query for Record Id using BreezeJS如何使用 BreezeJS 查询记录 ID
【发布时间】:2015-04-23 02:17:16
【问题描述】:

我用微风创建了一个新条目,在提交时我想立即使用我用来创建记录的基本字段(例如使用电子邮件)获取记录的 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;
        }
    }

从上面的查询并不总是得到任何返回值

谢谢

【问题讨论】:

  • 我需要一个关于如何通过谓词查询来获取记录 id 的示例查询。

标签: breeze


【解决方案1】:

不完全确定我是否理解您的问题,但听起来您想在保存后立即获取新保存记录的 ID。如果是这样,那么下面的答案适用。

当保存承诺解决时,它会返回已保存实体的列表以及一个 keyMappings 数组,用于显示因保存而 id 发生更改的任何实体。即从临时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();

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2015-01-01
    • 1970-01-01
    • 2010-10-17
    相关资源
    最近更新 更多