How to redirect to a nested dynamic segments in EMberjs - redirect

If we have several books,each books contains several chapters and each chapter contains several pages.
In an App,
when user navigate to
"/home"
list all the books,clicking a book(eg:book_1) will directly "linkTo"
"/book_1/chapter_1/page_1"
show the content of "chapter_1/page_1" within the selected book.
I am now trying to use the "redirect" hook,but I am confused with these:
1,How to sent muti params to a redirect hook;
2,How to update the URL correctly after redirecting?
3,What is the "Ember way" to do this?
_BOOK MODEL__
App.Book = DS.Model.extend({
name: DS.attr('string'),
chapters: DS.hasMany('chapter',{async:true}),
color: DS.attr('')
});
App.Chapter = DS.Model.extend({
name: DS.attr('string'),
author: DS.attr(''),
pages: DS.hasMany('page',{async:true})
});
App.Page = DS.Model.extend({
name: DS.attr('string'),
});
App.Book.FIXTURES = [
{
id: 1,
name: 'book1',
color: 'red',
chapters: [1,2]
},
{
id: 2,
name: 'book2',
color: 'blue',
chapters: [3,4]
}
];
App.Chapter.FIXTURES = [
{
id: 1,
name: 'Chapter1',
author: 'jhon',
pages:[1,2]
},
{
id: 2,
name: 'Chapter2',
author: 'jhon',
pages:[3,4]
},
{
id: 3,
name: 'Chapter3',
author: 'peter',
pages:[5,6]
},
{
id: 4,
name: 'Chapter4',
author: 'tom',
pages:[7,8]
}
];
App.Page.FIXTURES = [
{
id: 1,
name: 'page1',
},
{
id: 2,
name: 'page2',
},
{
id: 3,
name: 'page3',
},
{
id: 4,
name: 'page4',
},
{
id: 5,
name: 'page5',
},
{
id: 6,
name: 'page6',
},
{
id: 7,
name: 'page7',
},
{
id: 8,
name: 'page8',
}
];

redirect is deprecated, use the afterModel hook. I'm sending in ids of 1 and 1 to the chapter and page route, which should hit their model hooks. How is your data separated, does the book model have all the necessary info?
http://emberjs.jsbin.com/uDiLIkij/6/edit
afterModel: function(model, transition){
console.log(transition.targetName);
if(transition.targetName=='book.index'){
var self = this;
model.get('chapters').then(function(chs){
var ch = chs.get('firstObject');
ch.get('pages').then(function(pages){
var page = pages.get('firstObject');
self.transitionTo('page', model, ch, page);
});
});
transition.abort();
}
},

Related

Filter documents that do not have a property set - Prisma

I have this example data:
const example = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
name: "Vereth",
age: 19
},
{
name: "Mikaela",
age: 17
},
{
name: "Richard",
age: 20
},
{
productId: 4,
name: "Markus",
age: 24
},
{
productId: 8,
name: "Cecilia",
age: 18
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(example)
I have a 'User' model in prisma. So what I must achieve is to filter all those users that do not have a defined productId and also filter by productId.
For example, I want to filter all users that have product Id 1 and also all users that do not have a productId defined
I have tried with the following filter
this.prisma.users.findMany({
where: {
OR: [
{ productId: undefined },
{ productId: 1 }
]
}
})
This query gets me all the users that have '1' as the value of their 'productId' but it does not bring me the users that do not have a defined productId.
The expected result of the above query would be:
const result = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
name: "Vereth",
age: 19
},
{
name: "Mikaela",
age: 17
},
{
name: "Richard",
age: 20
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(result)
I only get the users 'Husdady' and 'Anton'
const resultObtained = [
{
productId: 1,
name: "Husdady",
age: 21
},
{
productId: 1,
name: "Anton",
age: 16
}
]
console.log(resultObtained)
How I can resolve this problem?

Mongoose: Get the data from 2 collections, like get products that have same category type like fruits

I am very new to mongoDB and mongoose and I also know questions like this has been already asked but still I didn't get any luck.
So what I am trying to do is I have 2 collections one is products and second is categories and I am trying to get the products that are of category type: fruits
product.model.js
const mongoose = require('mongoose')
const productSchema = new mongoose.Schema({
category: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category'
},
name: {
type: String,
required: true,
unique: true
},
price: {
type: Number,
required: true
},
image: {
type: String,
required: true
},
description: {
type: String,
required: true
}
})
const Product = mongoose.model('Product', productSchema)
module.exports = Product
category.model.js
const mongoose = require('mongoose')
const categorySchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
lowercase: true
}
})
const Category = mongoose.model('Category', categorySchema)
module.exports = Category
products collecction
[ { _id: 5ffc9795355f3707ec326f51,
name: 'apple',
price: 80,
image: 'apple1.jpg',
description:
'Do you really need description for this ??',
category: 5ffc94e8a7542a37101a119c,
__v: 0 },
{ _id: 5ffd54409793c33b183376b8,
name: 'Beef',
price: 180,
image: 'beef1.jpg',
description: 'Don\'t you dare to ask about beef in india',
category: 5ffc94dda7542a37101a119b,
__v: 0 },
{ _id: 5ffd54969793c33b183376b9,
name: 'Mango',
price: 100,
image: 'mango1.jpg',
description:
'Have some manog because you cannot eat the bottle of slice like katrina kaif do, its crazy',
category: 5ffc94e8a7542a37101a119c,
__v: 0 } ]
categories collection
[ { _id: 5ffc947ea7542a37101a119a, name: 'vegitables', __v: 0 },
{ _id: 5ffc94dda7542a37101a119b, name: 'non-veg', __v: 0 },
{ _id: 5ffc94e8a7542a37101a119c, name: 'fruits', __v: 0 } ]
Now I want to the get the products whose category are fruits
I know I have to use aggregation and lookup for this I also tried but didn't get any luck, this is my code
const Product = require('./models/product')
const Category = require('./models/category')
const test = async () => {
try {
var query = [
{
$lookup: {
from: "product",
localField: "category",
foreignField: "_id",
as: "product"
}
}]
const productCate = await Product.aggregate(query)
console.log(productCate)
// const productCat = await Product.find().populate({
// path: 'category',
// match: {name: 'fruits'}
// }).exec()
// console.log(productCat)
}catch(e) {
}
}
test()
UPDATE-1
I also tried ppuplate but still didn't get the response as expected, this is my code:
const test = async () => {
try {
const productCat = await Product.find().populate({
path: 'category',
match: {name: 'fruits'}
}).exec()
console.log(productCat)
}catch(e) {
}
}
test()
this is my output
[ { _id: 5ffc9795355f3707ec326f51,
name: 'apple',
price: 80,
image: 'apple1.jpg',
description:
'Do you really need description for this, how dumb are you ??',
category: { _id: 5ffc94e8a7542a37101a119c, name: 'fruits', __v: 0 },
__v: 0 },
{ _id: 5ffd54409793c33b183376b8,
name: 'Beef',
price: 180,
image: 'beef1.jpg',
description: 'Don\'t you dare to ask about beef in india',
category: null,
__v: 0 },
{ _id: 5ffd54969793c33b183376b9,
name: 'Mango',
price: 100,
image: 'mango1.jpg',
description:
'Have some manog because you cannot eat the bottle of slice like katrina kaif do, its crazy',
category: { _id: 5ffc94e8a7542a37101a119c, name: 'fruits', __v: 0 },
__v: 0 } ]
Here I also get the beef that is fall under non-veg category but with the category null. Actually I only want the fruits category products.
you can following this code
const test = async () => {
try {
let categoryId = await Category.findOne({name:"fruits"}).select("_id")
const productCat = await Product.find({category:categoryId}).populate(
"category"
).lean()
console.log(productCat)
}catch(e) {
}
}
test()
you can do with with populate match without using aggregation:
Product.find().populate({
path: 'category',
match: { 'name': 'fruits' }
})
.exec()

In Sails.js v0.10 after populating data as a value i get [Getter/Setter]

After populating data i get result not as expected.
Example:
//User.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
schools: { collection: 'School', via: 'id' },
name: { type: 'String', trim: true, required: true }
}
}
And this is my School.js
//School.js Model
module.exports = {
attributes: {
//"id" attribute is here primary key by default
name: { type: 'String', trim: true, required: true },
}
}
My User entity data looks like this:
//User document in MongoDB
{
_id: 1,
name: "Foo",
schools: [1,2,3]
}
My School entity data looks like this:
//School documents in MongoDB
{
_id: 1,
name: "School 1"
}
{
_id: 2,
name: "School 2"
}
{
_id: 3,
name: "School 3"
}
Now i want to populate the schools. I do it like this:
User.find().populate("schools").exec(function(err, res){ console.log(res[0]) });
And this is what i get as a result:
{
schools: [Getter/Setter],
name: "Foo",
id: 1
}
Expected:
{
schools: [
{id: 1, name: "School 1"},
{id: 2, name: "School 2"},
{id: 3, name: "School 3"}
],
name: "Foo",
id: 1
}
How can i get expected results?
I use MongoDB as data storage.
Versions:
Sails.js: v0.10.0-rc5
Waterline: v0.10.0-rc7
sails-mongo: 0.10.0-rc2
Those are the expected results when using console.log! If you want to see the expanded object, do:
console.log(res[0].toObject());

Ember-data: Best Json related resources

Hi I am wondering which is the best solution to work with related resources in ember-data.
In order to build a REST backend.
1) embedded resources
user: {
id: "foo",
albums: [
album: {
id: "foo",
name: "bar",
description: "foobar",
},
album: {
id: "foobar"
name: "foobarfoo",
description: "fumanchu",
},
],
}
2) embedded id of the resources
user: {
id: "foo",
albums: [
album: {
id: "foo",
},
album: {
id: "foobar"
},
],
}
3) linked resources
user: {
id: "blabla",
links: {
"self": { "href": "/users/blabla" },
"albums": { "href": "/albums/blabla" }
},
}
Do you think is possible to use hipermedia as in HATEOAS?
Thanks in advance
Did you have a look at ActiveModelSerializers gem?
It is the easier way to get the job done when working with Rails on the server side.
For relations, it may embed or use references to sub-resources. References (which I would recommend) are serialized this way:
{
users: [{
id: 42,
firstName: "John",
albums: [ 1, 2, 3 ]
}]
}
{
albums: [{
id: 1,
name: "Foo"
}, {
id: 2,
name: "Bar"
}, {
id: 3,
name: "Buzz"
}]
}
Here, albums may also be included as sideloaded data in the first hash:
{
users: [{
id: 42,
firstName: "John",
albums: [ 1, 2, 3 ]
}],
albums: [{
id: 1,
name: "Foo"
...
}]
}

Chained Selectfields -- Sencha Touch 2.0

I am using the Sencha Touch 2.0 KitchenSink example to try to learn some of the basics. I am getting stuck on chained selectfields though.
I am going off the tutorial here but with the different setup I am being thrown off. The code below runs perfectly in the Forms.js file in kitchensink, but I am missing on the actual chainedselect part of it.
EDIT: What I am asking is how to make chained selectfields. I have the datastores and the selectfields in the example code, but not a working chained selectfield from first to second.
Ext.regModel('First', {
idProperty: 'FirstID',
fields: [{
name: 'FirstID',
type: 'int'
}, {
name: 'FirstName',
type: 'string'
}]
});
Ext.regModel('Second', {
idProperty: 'SecondID',
fields: [{
name: 'SecondID',
type: 'int'
},{
name: 'FirstID',
type: 'int'
}, {
name: 'SecondName',
type: 'string'
}]
});
var firstStore = new Ext.data.Store({
model: 'First',
data: [{
FirstID: 1,
FirstName: 'Kenworth'
}, {
FirstID: 2,
FirstName: 'Peterbilt'
}],
autoLoad: true
});
var secondStore = new Ext.data.Store({
model: 'First',
data: [{
SecondID: 1,
FirstID: 1,
SecondName: 'T800'
}, {
SecondID: 2,
FirstID: 1,
SecondName: 'T700'
}, {
SecondID: 3,
FirstID: 1,
SecondName: 'T660'
}, {
SecondID: 4,
FirstID: 1,
SecondName: 'T470'
}],
autoLoad: true
});
Ext.define('Kitchensink.view.Forms', {
extend: 'Ext.tab.Panel',
requires: [
'Ext.form.Panel',
'Ext.form.FieldSet',
'Ext.field.Number',
'Ext.field.Spinner',
'Ext.field.DatePicker',
'Ext.field.Select',
'Ext.field.Hidden'
],
config: {
activeItem: 0,
tabBar: {
// docked: 'bottom',
ui: 'dark',
layout: {
pack: 'center'
}
},
items: [
{
title: 'Basic',
xtype: 'formpanel',
id: 'basicform',
iconCls: 'refresh',
items: [
{
xtype: 'fieldset',
title: 'Enter Data',
instructions: 'Please enter the information above.',
defaults: {
labelWidth: '35%'
},
items: [
{
xtype: 'selectfield',
name: 'firstfield',
label: 'First',
store: firstStore,
displayField: 'FirstName',
valueField: 'FirstID'
}, {
xtype: 'selectfield',
name: 'secondfield',
label: 'Second',
store: secondStore,
displayField: 'SecondName',
valueField: 'SecondID'
}
]
}
]
}
]
},
onFirstChange: function(selectField, value){
var secondSelectField = this.items.get(1);
secondSelectField.store.clearFilter(); // remove the previous filter
// Apply the selected Country's ID as the new Filter
secondSelectField.store.filter('FirstID', value);
// Select the first City in the List if there is one, otherwise set the value to an empty string
var firstValue = secondSelectField.store.getAt(0);
if(firstValue){
secondSelectField.setValue(firstValue.data.SecondID);
} else {
secondSelectField.setValue('');
}
}
});