I am building an AngularJS CRUD site to add data to Parse.com to be used by mobile apps that will read it. (The CRUD is easier to do on a real keyboard due to the amount of data added.) First step is to create and save the Child objects (Ingredients and Steps), and then to create and save a Recipe object that has a Relation to the Ingredients and Steps. I need to use Relation and not Pointers for these. (because I may need this to be many to many in future)
我正在构建一个AngularJS CRUD站点,将数据添加到Parse.com,供将要读取它的移动应用程序使用。 (由于添加了大量数据,CRUD在真实键盘上更容易。)第一步是创建并保存子对象(成分和步骤),然后创建并保存具有关系的Recipe对象成分和步骤。我需要使用Relation而不是Pointers。 (因为将来我可能需要这么多)
Here's my problem: writing the query to find any Ingredients and Steps that were created that are NOT yet part of a relation, to find the ones to be added to a new recipe.
这是我的问题:编写查询以查找已创建但尚未成为关系的任何成分和步骤,以找到要添加到新配方中的成分和步骤。
The examples in the Javascript SDK don't show this scenario, they only show a query for a relation that exists, but does not have an additional attribute on the related item (comments for posts where post doesn't have an image).
Javascript SDK中的示例不显示此场景,它们仅显示存在的关系的查询,但在相关项目上没有其他属性(对于帖子没有图像的帖子的评论)。
Recipe has ingredients, a Relation<Ingredient>, and steps, a Relation<Step>.
This doesn't work to get the Ingredients that are not yet related to a Recipe.
这不适用于获取尚未与食谱相关的成分。
var Recipe = Parse.Object.extend("Recipe");
var Ingr = Parse.Object.extend("Ingredient");
recipeQuery= new Parse.Query(Recipe);
recipeQuery.exists("ingredients");
query = new Parse.Query(Ingr);
query.doesNotMatchQuery("recipe",recipeQuery);
query.ascending('name');
query.find({
success: function (data) {
if (data.length > 0) {
$scope.loadingMsg = '';
}
$scope.ingredients = data;
}
});
1 个解决方案
#1
1
angular
.module('app.services')
.service('siteService', function($q, ParseService, $timeout) {
var that = this;
this.fetchIngr = function() {
return this.fetchRecipes().then(function(recipes) {
var q = recipes.reduce(function(initial, current) {
initial.push(that.fetchIngredient(current));
return initial;
}, []);
return $q.all(q);
});
};
this.fetchRecipes = function() {
var defer = $q.defer();
var Recipe = Parse.Object.extend("Recipe");
recipeQuery = new Parse.Query(Recipe);
recipeQuery.ascending('name');
recipeQuery.find({
success: function(data) {
console.log('data', data)
defer.resolve(data);
},
error: function(error) {
defer.reject(error);
}
});
return defer.promise;
};
this.fetchIngredient = function(recipe) {
var defer = $q.defer();
recipe.relation('ingredients').query().find({
success: function(res) {
defer.resolve(res);
},
error: function(error) {
defer.reject(error);
}
});
return defer.promise;
};
})
Usage:
siteService.fetchIngr().then(function(all){
console.log('flatten all ingr for used recipes',[].concat.apply([], all));
});
in the fetchIngr function you will receive array of all ingredients that are used in the Recipe. then you have to the diff between two arrays to find not used ingredients. To do the subtract you should fetch all ingredients and to make subtraction between two arrays (one used ingrdients for recipes and the one with all ingredients). see here the post : JavaScript array difference
在fetchIngr函数中,您将收到食谱中使用的所有成分的数组。然后你必须在两个数组之间的差异找到未使用的成分。要进行减法,你应该获取所有成分并在两个数组之间进行减法(一个用于食谱的食物和用于所有成分的食物)。看到这里的帖子:JavaScript数组差异
if you have questions feel free to ask me.
如果您有任何问题,请随时问我。
#1
1
angular
.module('app.services')
.service('siteService', function($q, ParseService, $timeout) {
var that = this;
this.fetchIngr = function() {
return this.fetchRecipes().then(function(recipes) {
var q = recipes.reduce(function(initial, current) {
initial.push(that.fetchIngredient(current));
return initial;
}, []);
return $q.all(q);
});
};
this.fetchRecipes = function() {
var defer = $q.defer();
var Recipe = Parse.Object.extend("Recipe");
recipeQuery = new Parse.Query(Recipe);
recipeQuery.ascending('name');
recipeQuery.find({
success: function(data) {
console.log('data', data)
defer.resolve(data);
},
error: function(error) {
defer.reject(error);
}
});
return defer.promise;
};
this.fetchIngredient = function(recipe) {
var defer = $q.defer();
recipe.relation('ingredients').query().find({
success: function(res) {
defer.resolve(res);
},
error: function(error) {
defer.reject(error);
}
});
return defer.promise;
};
})
Usage:
siteService.fetchIngr().then(function(all){
console.log('flatten all ingr for used recipes',[].concat.apply([], all));
});
in the fetchIngr function you will receive array of all ingredients that are used in the Recipe. then you have to the diff between two arrays to find not used ingredients. To do the subtract you should fetch all ingredients and to make subtraction between two arrays (one used ingrdients for recipes and the one with all ingredients). see here the post : JavaScript array difference
在fetchIngr函数中,您将收到食谱中使用的所有成分的数组。然后你必须在两个数组之间的差异找到未使用的成分。要进行减法,你应该获取所有成分并在两个数组之间进行减法(一个用于食谱的食物和用于所有成分的食物)。看到这里的帖子:JavaScript数组差异
if you have questions feel free to ask me.
如果您有任何问题,请随时问我。