How to create a model instance that has a belongsTo property? - ember-data

var attr = DS.attr,
hasMany = DS.hasMany,
belongsTo = DS.belongsTo;
Admin.Category = DS.Model.extend({
name: attr(),
mstore: belongsTo('mstore')
});
console.log(mstore); // this is a PromiseObject object passed from "{{action createCategory mstore}}" tag
var newCategory = this.store.createRecord('category', {
name: 'nn',
mstore: mstore
});
I get an error like:
Assertion failed: You can only add a 'mstore' record to this relationship.
How can I set a belongsTo property using a PromiseObject object? Thanks.

In your {{action...}} you should pass a real model, not a promise. To get a model from a promise you need to do something like this:
var myMstore;
that.store.find('mstore', mstoreId).then(function(mstore) {
myMstore = mstore;
});

Related

VueJS router $routes.push can't push query params

Why on earth this doesn't work?
selectChanged(val) {
let term_ids = [];
let taxonomy = '';
val.forEach((obj) => {
term_ids.push(obj.term_id);
taxonomy = obj.taxonomy;
});
let obj = this.$route.query;
obj[taxonomy] = term_ids.join(',');
this.$router.push({
query: obj,
});
},
obj looks like this:
{education_levels: "33,36", candidate_countries: "304"}
If I hardcode above object it will work as intended, VueJS Router will push query string and it will look like this: ?education_levels=33,36&candidate_countries=304
But if I pass query: obj nothing will happen...
You need to assign all the properties with route.
Object.Assign makes a new COPY of the of the $route, including all enumerable OWN properties
One more suggestion -
When you do this let obj = this.$route.query; -
Objects get stored to memory so when you try to copy an object you are actually copying the address of the object where that stored in memory.
So when you do this - obj[taxonomy] = term_ids.join(',');
You will accidently mutate the original value, so cloning required
Instead clone it-
let obj = { ... this.$route.query }
this.$router.push({
query: Object.assign({}, this.$route.query, obj),
});

How to get odoo model from javascript?

I'm trying to do a widget to attach to the sysTrayMenu, I need to know on the on_click event, the current model of the view. I know that I can get it from the current browser url, but I wanted to know if is there a cleaner way to get it from the odoo js api.
For example if the user is in New quotation menu, I need to get sale.order
odoo.define('xx.systray', function (require) {
"use strict";
var config = require('web.config');
var SystrayMenu = require('web.SystrayMenu');
var Widget = require('web.Widget');
var ajax = require('web.ajax');
var xxMenu = Widget.extend({
template:'solvo-support.helpMenu',
events: {
"click": "on_click",
},
on_click: function () {
//HERE I NEED TO GET THE CURRENT MODEL
},
});
SystrayMenu.Items.push(xxMenu);
});
As I remember you can access the the model of the widget like this:
this.model // or self.model if you defined self (self = this)
all widget have this attribute it's string type contains the name of the model.

find subdocument without start from parent

Pretty new to mongoose. Can I findOne off of a subdocument model?
I have a subdoc called deliverables which is a child of projects
What I'd like to do is FIND on my deliverables model so I don't have to find on the project as
{project.child.child.child.deliverables._id: req.id}
Is that possible or do I have to start from the project model each time? Below is a sample setup I'm using along with my example findOne.
'use strict';
//////////////model/////////////////
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var deliverablesSchema = new Schema({
title:{
type:String,
}
})
var ProjectSchema = new Schema({
name: {
type: String,
},
deliverables: [deliverablesSchema],
});
mongoose.model('Deliverable', deliverablesSchema);
mongoose.model('Project', ProjectSchema);
//////////////controller/////////////////
var mongoose = require('mongoose'),
Project = mongoose.model('Project'),
Deliverable = mongoose.model('Deliverable'),
_ = require('lodash');
exports.findDeliverable = function(req, res) {
Deliverable.findOne({'_id': req.params.deliverableId}).exec(function(err, deliverable) {
if(deliverable){
//return
}
});
};
You can find subdocuments within a document only if you have declared their schema http://mongoosejs.com/docs/subdocs.html
Here is an example taken from here:
Project.findOne({
'_id': myid
}).exec(function (err, p) {
if (p) {
//return
var deriv = p.deliverables.filter(function (oneP) {
return oneP._id === 'myderivableid';
}).pop();
}
});
If your subdocuments are just nested objects in an array you may use Lodash to retrieve that data using _ .where or _.find
Kept digging and found this:
https://stackoverflow.com/a/28952991/2453687
You still have to pull the master document first but it's easy to drill down to the particular object you're looking for and make a quick update, then just update the whole master document in one shot.
Dead simple. Works like a charm.

get is not defined when trying to extends JSONSerializer

I try to define my custom serializer by extending DS.JSONSerialzer.
I pick the serialize function without modifications but when i run Ember,i get this error:
ReferenceError: get is not defined
This is my code :
import DS from 'ember-data';
export default DS.JSONSerializer.extend({
serialize: function(record, options) {
var json = {};
if (options && options.includeId) {
var id = get(record, 'id');
if (id) {
json[get(this, 'primaryKey')] = id;
}
}
record.eachAttribute(function(key, attribute) {
this.serializeAttribute(record, json, key, attribute);
}, this);
record.eachRelationship(function(key, relationship) {
if (relationship.kind === 'belongsTo') {
this.serializeBelongsTo(record, json, relationship);
} else if (relationship.kind === 'hasMany') {
this.serializeHasMany(record, json, relationship);
}
}, this);
return json;
},
});
I didn't change any code. This is the original. Why get is suddenly undefined? It's imported in line 1 in the original file JSONSerialiser
Can you help me?
They have get defined in the scope when creating the serializer, but that doesn't extend outside of their scope into your files.
var get = Ember.get;
var isNone = Ember.isNone;
var map = Ember.ArrayPolyfills.map;
var merge = Ember.merge;
Either replace all of the get methods with Ember.get or define get to be Ember.get

extjs checkbox grid delete rails

i am using ExtJS with Rails...I am trying to delete records selected in grid through "Checkbox column"...i dnt have any idea as to how can i handle "Array" of selected records of grid through rails controller...plzz guide me...
the code on delete button is as follows :
var sm = prodgrid.getSelectionModel();
delbtn.on("click", function () {
var sel = sm.getSelections();
Ext.Ajax.request({
url: 'products/delete',
// method:'DELETE',
params: {
'prodid': sel
}
});
});
How can i iterate through "sel" array in my Rails controller?? plzz help
use Ext.each to iterate an array :
var sm = prodgrid.getSelectionModel();
delbtn.on("click", function () {
var sel = sm.getSelections();
Ext.each(sel,function(data){
/// your stuff
Ext.Ajax.request({
url: 'products/delete',
// method:'DELETE',
params: {
'prodid': data.id // the parameter
}
});
///// end
},this);
});
You cannot pass arrays into Rails controller directly. This article should help you in understanding parameter passing into rails controllers.
That said, you need to convert the array into a string. You can use a function similar to this for converting the array to string:
function array_params(arry) {
var paramvar = "";
arry.each(function(s){
paramvar = paramvar.concat("arr[]=",s,"&");});
paramvar = paramvar.replace(/&$/,"");
return paramvar;
}
and finally call:
Ext.Ajax.request({
url: 'products/delete',
// method:'DELETE',
params: {
'prodid': array_params(sel)
}
});