已更新,提供了对建议答案的回复,正在等待赏金奖励之前的回复。
使用Parse.com和JavaScript SDK。
我似乎无法解决此问题,因此将其悬赏。对于悬赏,我想要一个可以解决问题的代码示例/解决方案,我可以从中学习。
以下代码将对象保存到“mybadges”类。我想在“FriendRequest”类中将一个指针或关系连接到mybadges类。这意味着,当我单击“FriendRequests”中的指针或关系时,它将返回上载到myBadges类中的所有对象。然后,我希望能够通过查询访问此数据。
使用Parse.com和JavaScript SDK。
Parse.initialize("xxx", "xxx");
var user = Parse.User.current();
var MyBadges = Parse.Object.extend("myBadges");
var userbadges = new MyBadges();
var Friends = Parse.Object.extend("FriendRequest");
var friends = new Friends();
//var Badges = Parse.Object.extend("myBadges");
//var Badges = Parse.Object.extend("myBadges");
$(document).ready(function () {
$("#send").click(function () {
var badgeselected = $('#badgeselect .go').attr("src");
userbadges.set("BadgeName", badgeselected);
userbadges.set("uploadedBy", user);
//friends.set("status");
//friends.save();
userbadges.save(null, {
success: function (results) {
// The object was saved successfully.
friends.relation('Friends').add(userbadges);
//friends.save();
console.log(user);
//location.reload();
},
error: function (contact, error) {
// The save failed.
// error is a Parse.Error with an error code and description.
alert("Error: " + error.code + " " + error.message);
}
});
});
});
$(document).ready(function() {
$('.go').css('cursor', 'pointer');
$('.go').click(function(e) { // Button which will activate our modal
$(this).width(100).height(100).appendTo('#badgeselect');
$('#modal').reveal({ // The item which will be opened with reveal
animation: 'fade', // fade, fadeAndPop, none
animationspeed: 600, // how fast animtions are
closeonbackgroundclick: true, // if you click background will modal close?
dismissmodalclass: 'close' // the class of a button or element that will close an open modal
});
return false;
});
});
最佳答案
要创建一对多关系,有两种选择:
friendRequest
指针添加到myBadges
类(仅支持一对多)最简单的选择是使用Array列,在JavaScript中,您仅使用包含
myBadges
实例的普通数组并将其分配给父类的属性。 注意:在下面的示例中,为了保持一致性,我将 call 类的大小写更改为UpperCamel单数(myBadges-> MyBadge),并将所有属性/列名称更改为LowerCamel(BadgeName-> badgeName),照着做。
示例:
假定的通用代码如下:
var FriendRequest = Parse.Object.extend("FriendRequest");
var MyBadge = Parse.Object.extend("MyBadge");
var friendRequest = /* some FriendRequest you've loaded */
选项1-反向引用
向 friend 请求添加新徽章:
// MyBadge.friendRequest: Pointer (to FriendRequest)
var myBadge = new MyBadge();
myBadge.set({
badgeName: someStringVariable,
uploadedBy: someUserObject,
friendRequest: friendRequest
});
myBadge.save();
获取 friend 请求的徽章:
var badgesQuery = new Parse.Query(MyBadge);
badgesQuery.equalTo('friendRequest', friendRequest);
badgesQuery.find({
success: function(badges) {
// use badges as needed...
}
});
在数据浏览器中显示FriendRequest父项的MyBadge行:
获取FriendRequest行的
objectId
,然后在MyBadge
的 View 上的friendRequest
列上添加过滤器并粘贴ID。选项2-数组
向 friend 请求添加新徽章:
// FriendRequest.badges: Array (of MyBadge)
// 2-step process, first create and save the MyBadge instance
var myBadge = new MyBadge();
myBadge.set({
badgeName: someStringVariable,
uploadedBy: someUserObject
});
myBadge.save()
// step 2 (using chaining here) attach the saved badge to the friend request
.then(function() {
friendRequest.badges.addUnique(myBadge);
// return promise to allow further chaining
return friendRequest.save();
});
获取 friend 请求的徽章:
// just include them when querying for FriendRequest objects
friendRequestQuery.include('badges');
在数据浏览器中显示FriendRequest父项的MyBadge行:
没有对此的支持,您将需要创建自己的解决方案。
选项3-关系
添加新徽章:
// FriendRequest.badges: Relation (of MyBadge)
// 2-step process, first create and save the MyBadge instance
var myBadge = new MyBadge();
myBadge.set({
badgeName: someStringVariable,
uploadedBy: someUserObject
});
myBadge.save()
// step 2 (using chaining here) attach the saved badge to the friend request
.then(function() {
var badgesRelation = friendRequest.relation('badges');
badgesRelation.add(myBadge);
// return promise to allow further chaining
return friendRequest.save();
});
获取 friend 请求的徽章:
// query the relation
var badgesRelation = friendRequest.relation('badges');
var badgesQuery = badgesRelation.query();
// apply any filters as you would for any normal Parse.Query()
// ...
// use find() or any other query method
badgesQuery.find({
success: function(badges) {
// use array of badges...
}
})
在数据浏览器中显示FriendRequest父项的MyBadge行:
双击
badges
列中的值。选项4-自定义加入类
这确实不适用于您的用例,因此在这里我将不提供示例。