更新后,回复所提供的建议答案,等待赏金奖励前的回复。
使用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;
});
});
答案 0 :(得分:4)
要创建一对多关系,有几个选项:
friendRequest
类添加myBadges
指针(仅支持一对多)myBadges
个实例的普通数组,并将其分配给父类的属性。注意:在下面的示例中,我将把调用类的大小写改为UpperCamel singular(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 - 反向引用
向朋友请求添加新徽章:
// MyBadge.friendRequest: Pointer (to FriendRequest)
var myBadge = new MyBadge();
myBadge.set({
badgeName: someStringVariable,
uploadedBy: someUserObject,
friendRequest: friendRequest
});
myBadge.save();
获取朋友请求的徽章:
var badgesQuery = new Parse.Query(MyBadge);
badgesQuery.equalTo('friendRequest', friendRequest);
badgesQuery.find({
success: function(badges) {
// use badges as needed...
}
});
在数据浏览器中显示FriendRequest父级的MyBadge行:
获取FriendRequest行的objectId
,然后在MyBadge
的视图上添加friendRequest
列上的过滤器并粘贴ID。
选项2 - 数组
向朋友请求添加新徽章:
// 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();
});
获取朋友请求的徽章:
// 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();
});
获取朋友请求的徽章:
// 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 - 自定义加入类
这并不适合您的使用案例,所以我不打算在这里提供样本。