Parse ember Rest adapter hasMany relation ship ember-parse-adapter - ember.js

Hi I am unable to get hasMany relationship right in parse ember adapter here is my code.
var newpost = this.store.createRecord('post', {
text: 'mypost',
});
newpost.save().then(function () {
var newcomment = this.store.createRecord('comment', {
text: 'mycomment'
});
newcomment.set('post', this.store.find('post', newpost));
newcomment.save();
})
My url Query parameters looks like this in chrome
where[$relatedTo][object][__type]:Pointer
where[$relatedTo][object][className]:Post
where[$relatedTo][object][objectId]:2KdEkYmOGX
where[$relatedTo][key]:comments
and I get this error
{"code":102,"error":"wrong type of relation. Expecting: , but recieved: Comment"}
my models are basic
Post
import ParseModel from 'appkit/models/parsemodel';
var post = ParseModel.extend({
text: DS.attr('string'),
comments : DS.hasMany( 'comment', { async : true } )
});
export default post;
comment model
import ParseModel from 'appkit/models/parsemodel';
var comment = ParseModel.extend({
text: DS.attr('string'),
post : DS.belongsTo( 'post' )
});
export default comment;

Related

How to organize my model

i try to build a simple chat. User can select another one to talk with it. I use Ember with firebase. I've build my model like firebase example.
This is my simple model.
User Model :
import DS from "ember-data";
var user = DS.Model.extend({
name : DS.attr('string'),
messages : DS.hasMany("message", {async : true, inverse : 'owner'})
});
export default user;
Message Model :
import DS from "ember-data";
var message = DS.Model.extend({
date : DS.attr('date'),
content : DS.attr('string'),
owner : DS.belongsTo('user', {async : true}),
target: DS.belongsTo('user', {async : true})
});
export default message;
Emberfire doesn't support 'findQuery' ember-data type search, so how can i retrieve all messages that belong to a conversation? It is the right way to define my model or is there another one? In the ideal case, i would just want retrieve all message with a single request. ( from owner to target and from target to owner)
If you're sticking with the official emberfire bindings, you can set up three models:
User:
var user = DS.Model.extend({
name : DS.attr('string'),
conversations : DS.hasMany('conversation', { async: true }),
convos_users : DS.hasMany('convo_user', { embedded: true })
});
Conversation:
var conversation = DS.Model.extend({
messages : DS.hasMany('message', { embedded: true })
});
Message:
var message = DS.Model.extend({
date : DS.attr('date'),
content : DS.attr('string'),
from : DS.belongsTo('user', { async : true })
});
And then set up the embedded convos_users index:
var convos_users = DS.Model.extend({
with : DS.belongsTo('user', {async : true}),
conversation : DS.belongsTo('conversation', { async: true })
});
So the resulting schema looks something like this in firebase:
{
'users': {
'user_1': {
'name': 'Terrance',
'conversations': {
'convo_1': true
},
'convo_users': {
0: {
'with': 'user_2',
'conversation': 'convo_1'
},
...
}
},
'user_2': {
'name': 'Phillip',
'conversations': {
'convo_1': true
},
'convo_users': {
0: {
'with': 'user_1',
'conversation': 'convo_1'
},
...
}
},
...
},
'conversations': {
'convo_1': {
'messages': {
0: {
'date': 123456789,
'content': 'Hey buddy!',
'from': 'user_1'
},
1: {
'date': 123456789,
'content': 'Hey guy!',
'from': 'user_2'
},
...
}
}
}
}
This setup lets you embed messages together in a common conversation thread, so you only retrieve the messages for the conversation you want to see. The 'from' attribute in the message lets you render the user that it came from, and sort the alignment of the chat window, or whatever you're looking to do.
Finally, indexing both the list of conversations the user has ever been in, along with an index of the other user id in the conversation and that conversation's ID. This way, when user A goes to send a message to user B, you can do a computed findBy on the 'user_conversations' index. If a match exists, open the conversation with the conversation ID found, and append the messages to the conversation's embedded message array:
actions: {
sendMessage: function(msg) {
var userX = this.current_user.get('convos_users').findBy('with','user_X');
// No User
if (!userX) {
// 1. Create a new Conversation (var myRoom)
// 2. Save room id to users
// 3. Save room to your conversations model list
}
// Else
myRoom.messages.pushObject(msg);
myRoom.save();
}
}
}
Good luck!

Ember template doesn't update after saving model with EmberFire adapter for Firebase

I'm using Firebase as the backend for my Ember app and successfully hooked up Firebase with EmberFire. However, when I try to save my models after creating a record, while they are posted to Firebase, they are not updated in the client's ember template.
My code for the action handler before I realized this was:
actions: {
publishPost: function() {
var newPost = this.store.createRecord('post', {
title: this.get('post.title'),
body: this.get('post.body'),
timestamp: new Date()
});
newPost.save();
}
}
And my code to try and solve this (but that doesn't work) by catching the promise returned by the save() and find() methods is:
actions: {
publishPost: function() {
var newPost = this.store.createRecord('post', {
title: this.get('post.title'),
body: this.get('post.body'),
timestamp: new Date()
});
this.store.find('post').then(function(posts) {
posts.addObject(newPost);
posts.save().then(function() {
newPost.save();
});
});
}
}
Am I just not handling the logic in the above code correctly to update the template or is there another Firebase/EmberFire compatible solution for this?
On https://github.com/broerse/ember-cli-blog-fire I did this:
import Ember from "ember";
export default Ember.ObjectController.extend({
actions: {
createPost: function() {
var newPost = this.get('store').createRecord('post');
newPost.set('date' , new Date());
newPost.set('author' , 'C.L.I. Ember');
this.get('target').transitionTo('post', newPost.save());
}
}
});
transitionTo can handle the newPost.save() promise.
Try it on: http://exmer.com/bloggrfire/posts

Filtering child records in Ember Data

I have not been able to filter the childrecords of objects in ArrayController.
The structure of my models is like this:
var Shop = DS.Model.extend({
name: DS.attr('string'),
products: DS.hasMany('product')
});
var Product = DS.Model.extend({
name: DS.attr('string'),
shop: DS.belongsTo('shop')
});
Shop has many products, and product belongs to a shop. I would like to filter the childrecords of each parent based on a Ember.TextField. Filtering works if I'm only filtering the parent records based on a property they have, using a regexp.
productSearchResults: function() {
var productSearchTerm = this.get('productSearchTerm');
var regExp = new RegExp(productSearchTerm,'i');
Ember.Logger.log('productSearchTerm', productSearchTerm);
var filteredResults = this.map(function(shop){
var products = shop.get('products');
return products.filter(function(product){
regExp.test(product.get('name'));
});
});
// all items are returned always..
return filteredResults;
}.property('products.#each', 'productSearchTerm')
Edit
I tried to use promises here (source: Filter child-records (hasMany association) with Ember.js ), but it seems like this productSearchResults property is never accessed. I do not get any log output from here. In the template, I'm looping over filteredProducts and there is nothing there. If it's of any relevance, I'm using Ember 1.5.0 and Ember Data 1.0.0-beta.7+canary.b45e23ba .
productSearchResults: function() {
var _that = this;
var productSearchTerm = this.get('productSearchTerm');
var regExp = new RegExp(productSearchTerm,'i');
this.store.find('shop').then(function(shops) {
var promises = shops.map(function (shop) {
return Ember.RSVP.hash({
shop: shop,
products: shop.get('products').then(function (products) {
return products.filter(function (product) {
return regExp.test(product.name);
});
})
});
});
Ember.RSVP.all(promises).then(function (filteredProducts) {
_that.set('filteredProducts', filteredProducts);
});
});
}.property('products.#each', 'productSearchTerm')

Implementing model filter

I have set up the following scaffolding for my Ember application.
window.App = Ember.Application.create({});
App.Router.map(function () {
this.resource('coaches', function() {
this.resource('coach', {path: "/:person_id"});
});
});
App.ApplicationAdapter = DS.FixtureAdapter.extend({});
App.Person = DS.Model.extend({
fname: DS.attr('string')
,lname: DS.attr('string')
,sport: DS.attr('string')
,bio: DS.attr('string')
,coach: DS.attr('boolean')
,athlete: DS.attr('boolean')
});
App.Person.FIXTURES = [
{
id: 10
,fname: 'Jonny'
,lname: 'Batman'
,sport: 'Couch Luge'
,bio: 'Blah, blah, blah'
,coach: true
,athlete: true
}
,{
id: 11
,fname: 'Jimmy'
,lname: 'Falcon'
,sport: 'Cycling'
,bio: 'Yada, yada, yada'
,coach: false
,athlete: true
}
];
I am trying to set up a route to filter the person model and return only coaches. Just to make sure I can access the data, I have simply used a findAll on the person model.
App.CoachesRoute = Ember.Route.extend({
model: function() {
return this.store.findAll('person');
}
});
Now though, I am trying to implement the filter method detailed on the bottom of the Ember.js Models - FAQ page.
App.CoachesRoute = Ember.Route.extend({
model: function() {
var store = this.store;
return store.filter('coaches', { coach: true }, function(coaches) {
return coaches.get('isCoach');
});
}
});
The coaches route is not working at all with the new route implemented and the old one commented out. I am using the Ember Chrome extension and when using the filter route the console responds with, Error while loading route: Error: No model was found for 'coaches'. Apparently the route is not working, specifically the model. No kidding, right? What am I missing in my filter model route?
Thank you in advance for your help.
The error message is spot on- there is no CoachModel. I think you need to do this:
App.CoachesRoute = Ember.Route.extend({
model: function() {
var store = this.store;
return store.filter('person', { coach: true }, function(coaches) {
return coaches.get('isCoach');
});
}
});

Ember.js access model values

I'd like to be able to modify/validate data before actually saving.
Model
App.Post = DS.Model.extend({
title: DS.attr('string'),
author: DS.attr('string'),
date: DS.attr('date', { defaultValue: new Date() }),
excerpt: DS.attr('string'),
body: DS.attr('string')
});
Route
App.PostsNewRoute = Ember.Route.extend({
model: function() {
return this.get('store').createRecord('post');
},
actions: {
doneEditing: function() {
debugger;
this.modelFor('postsNew').save();
this.transitionTo('posts.index');
}
}
});
So, the questions, before the .save() I want to, let's say, validate that the title is not empty or so.
Everything I've tried gets undefined, or [Object object] has no .val() method. I don't know how to get to the values of the model. How can I do that?
And the other thing I have in mind. Is that defaultValue working as intended? I want to set Date() to every new created post. Somehow date is not being recorded since it's not showing.
Thanks.
App.PostsNewRoute = Ember.Route.extend({
model: function() {
return this.get('store').createRecord('post');
},
actions: {
doneEditing: function() {
debugger;
var model = this.modelFor('postsNew');
var title = model.get('title');
model.save();
this.transitionTo('posts.index');
}
}
});