I'm new to senach and ExtJs, and I'm trying to read and write json data. getting the data is already working, but I can't write changed data back to the server.
I'm using a ExtJs 4.2.
I configured one URL in the RestProxy url: /animals/dogs/
GET http://example.com/animals/dogs
PUThttp://example.com/animals/dogs/{id}
Get is working as expected. But the PUT URL is:
example.com/animals/dogs/project.model.dogs-3?dc435453...
The id also contains the model name. How can I change this to the id like above?
This is the store:
Ext.define('project.store.MyJsonStore', {
extend: 'Ext.data.Store',
requires: [
'project.model.dogs',
'Ext.data.proxy.Rest',
'Ext.data.reader.Json'
],
constructor: function(cfg) {
var me = this;
cfg = cfg || {};
me.callParent([Ext.apply({
autoLoad: true,
autoSync: true,
model: 'project.model.dogs',
storeId: 'MyJsonStore',
proxy: {
type: 'rest',
limitParam: 'count',
url: '/animals/dogs,
reader: {
type: 'json',
idProperty: 'id',
root: 'data'
}
}
}, cfg)]);
}
});
This is the model:
Ext.define('project.model.dogs', {
extend: 'Ext.data.Model',
requires: [
'Ext.data.Field'
],
clientIdProperty: 'id',
fields: [
{
name: 'id',
type: 'int'
},
{
name: 'type',
type: 'string'
}
]
});
GET Respones: (reader root is data)
{
"success": true,
"data":
[
{
"id": 1,
"type": "Some Type",
"url": "/animals/dogs/1"
},
{
"id": 2,
"type": "Some other Type",
"url": "/animals/dogs/2"
},
]
}
The PUT urls look like:
example.com/animals/dogs/project.model.dogs-3?dc...
example.com/animals/dogs/project.model.dogs-7?dc...
example.com/animals/dogs/project.model.dogs-8?dc...
The numbers 3, 7, 8 are the id's, so extjs recognizes the id property in the model.
Regards,
Sencha Beginner
Related
So, I have a model called Drivers that receive a field called "user", which references a document from another model, like this:
const DriversSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
},
{
timestamps: true,
}
);
// ...
Querying the collection works as expected, here's an example:
Drivers.find({});
// returns ->
[
{
"name": "John",
"user": "5e43f8ad2fbb1d0035d5a154",
}
]
Is there a way to return the actual document represented by the 'user' field?
Thanks!
I use mongoose. I tried to add models in
ProductController.js:
const student = require('../models/studentModel');.populate('student_list')
but I still get the same result.
productModel.js
{
lectureProductId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Lecture',
}
}
lectureModel.js
{
task_list: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'task'
}],
student_list: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'student'
}],
teacher: {
type: mongoose.Schema.Types.ObjectId,
ref: 'teacher', required: true,
},
}
productController.js
productModel.find()
.populate('lectureProductId')
.populate('taskProductId')
.then(product => {
res.json({
status: 'success',
message: 'products retrieved successfully',
data: product
});
})
lectureController.js
lectureModel.find()
.populate('teacher')
.populate('task_list')
.populate('student_list')
.then(lecture => {
res.json({
status: 'success',
message: 'Lectures retrieved successfully',
data: lecture,
});
})
When I send request I see :
"lectureProductId": {
"keywords": [ "MATLAB", "BİSECTİON", "NEWTON-RAHSON" ],
"task_list": [ "5d26d617454d23000665421c", "5d26d617454d230006654217", "5d26d617454d23000665421b", "5d26d617454d23000665421a", "5d26d617454d230006654218" ],
"student_list": [ "5d26d615454d230006654206", "5d26d615454d230006654207", "5d26d615454d230006654208", "5d26d615454d23000665420c", "5d26d615454d23000665420d", "5d26d616454d23000665420e", "5d26d616454d23000665420f" ],
"_id": "5d26d617454d230006654221",
"name": "Numerical Analysis",
"programmingLanguages": "MATLAB",
"description": "MATLAB for easy level",
"teacher": "5d26d616454d230006654215", "__v": 0
}
How can I see student_list with other fields?
"student_list": [
"5d26d615454d230006654206",
"5d26d615454d230006654207",
"5d26d615454d230006654208",
"5d26d615454d23000665420c",
"5d26d615454d23000665420d",
"5d26d616454d23000665420e",
"5d26d616454d23000665420f"
]
productModel
.find()
.populate({
path: 'lectureProductId',
model: 'lecture',
populate: { path: 'task_list' },
})
This is answer for my question.
productModel
.find()
.populate({
path: 'id with ref',
model: 'childs model',
populate: { path: 'id with ref in childs model' },
})
Can anyone explain me, what am I doing wrong?
I'm trying to load data to store and select some of that on form load.
Here is what I came up with so far:
https://fiddle.sencha.com/#fiddle/cjd
Ext.define('TagModel', {
extend: 'Ext.data.Model',
fields: [{
name: 'some_id',
type: 'int'
}, {
name: 'some_value',
type: 'string'
}]
});
Ext.define('MyPanel',{
extend: 'Ext.panel.Panel',
renderTo: Ext.getBody(),
title: 'Some title',
width: 200,
heigh: 500,
layout: 'anchor',
items: [{
xtype: 'tagfield',
anchor: '100%',
displayField: 'some_value',
valueField: 'some_id',
store: Ext.create('Ext.data.Store', {
model: 'TagModel',
data: [{
some_id: 0,
some_value: 'value0'
}, {
some_id: 1,
some_value: 'value1'
}]
}),
// value: ['0']
}]
});
Ext.application({
name: 'Fiddle',
launch: function() {
Ext.create('MyPanel');
}
});
It works well, but if you uncomment line 40, which should tell component to select items by their valueField config, it shows error in the console:
Uncaught TypeError: Cannot read property 'isModel' of undefined ext-all-debug.js:144157
According to the specification, value can be set as an array of strings associated to this field's configured valueField.
As posted by the OP Панов Владимир:
OK, I've found a solution:
The problem is in the source code of ExtJS. If anyone have same problem, you can use this override:
Ext.override(Ext.form.field.Tag, {
findRecord: function(field, value) {
var store = this.store,
matches;
if (store) {
matches = store.queryBy(function(rec) {
return rec.get(field) === value;
});
}
return matches ? matches.getAt(0) : false;
},
});
I'm getting the following error in my Ember.js app.
Error while processing route: books Cannot set property 'store' of undefined TypeError: Cannot set property 'store' of undefined
at DS.Store.Ember.Object.extend.modelFor (http://localhost:8080/js/libs/ember-data.js:2986:19)
at DS.Store.Ember.Object.extend.recordForId (http://localhost:8080/js/libs/ember-data.js:2437:17)
at deserializeRecordId (http://localhost:8080/js/libs/ember-data.js:3355:23)
at deserializeRecordIds (http://localhost:8080/js/libs/ember-data.js:3369:5)
at http://localhost:8080/js/libs/ember-data.js:3335:7
at http://localhost:8080/js/libs/ember-data.js:7117:16
at http://localhost:8080/js/libs/ember.js:14899:20
at Object.OrderedSet.forEach (http://localhost:8080/js/libs/ember.js:14741:14)
at Object.Map.forEach (http://localhost:8080/js/libs/ember.js:14897:14)
at Function.DS.Model.reopenClass.eachRelationship (http://localhost:8080/js/libs/ember-data.js:7116:38)
I'm using
ember.js version 1.7.1 and
ember-data.js version 1.0.0-beta.5.
I have the following project:
window.App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8080'
});
App.Router.map(function() {
this.resource('books', { path: '/' });
});
App.BooksRoute = Ember.Route.extend({
model: function() {
return this.store.find('book');
}
});
App.Author = DS.Model.extend({
firstname: DS.attr('string'),
lastname: DS.attr('string')
});
App.Book = DS.Model.extend({
title: DS.attr('string'),
authors: DS.hasMany('author')
});
and the following is my JSON response to http://localhost:8080/books.
{
"books":
[
{
"id": 0,
"authors":
[
{
"id": 0,
"firstname": "Andrzej",
"lastname": "Sapkowski"
}
],
"title": "Zaklínač I: Poslední přání"
},
...
]
}
When I remove the Author model and the authors relationship declaration, the application works fine.
The error message I'm getting doesn't reveal the cause and according to what I've found on the Internet, my code seems to be ok.
What's the problem?
Wow, I can't believe I didn't notice this, but your data isn't formatted properly. It should look something like this:
{
"books": [{}, {}, {}],
"authors": [{}, {}, {}]
}
This is explained in the REST adapter guide.
I'm new to Sencha, trying to implement a Sencha formpanel with a field to pick from a list of objects in a store. This seems like a sufficiently basic pattern that there must be a simple solution. I have a model which defines an ajax endpoint returning a json array (places) of name/id pairs:
Ext.define('MyApp.model.Place', {
extend: 'Ext.data.Model',
config: {
fields: [
{name: 'name', type: 'string'},
{name: 'id', type: 'int'},
],
proxy: {
type: 'ajax',
url: '/m/places/',
reader: {
type: 'json',
rootProperty: 'places',
},
},
},
});
And a store:
Ext.define('MyApp.store.Place', {
extend: 'Ext.data.Store',
requires: ['MyApp.model.Place'],
config: {
model: 'MyApp.model.Place',
autoLoad: true,
sorters: [
{
property : 'name',
direction: 'ASC',
},
],
},
});
I want a read-only text field for the place name and a hidden field to submit the place id:
{
xtype: 'textfield',
name: 'place_name',
label: 'Place',
readOnly: true,
listeners: {
focus: function () {
var place_picker = Ext.create('Ext.Picker', {
slots: [
{
name: 'place_name',
title: 'Choose a Place',
store: Ext.create('MyApp.store.Place'),
itemTpl: '{name}',
listeners: {
itemtap: function (obj, index, target, record, e, eOpts) {
var form = Ext.getCmp('entityform');
form.setValues({
place_name: record.get('name'),
place_id: record.get('id'),
});
// how to dismiss the picker?
obj.parent.hide();
},
},
},
]
});
Ext.Viewport.add(place_picker);
place_picker.show();
},
},
},
{
xtype: 'hiddenfield',
name: 'place_id',
},
At this point, tapping the field causes the picker to slide up from the bottom and display the loading animation, but it is not being populated with the list of place names, although I can see that the ajax request has been made and that it has returned the expected json document.
I'll stop here and ask if I'm on the right track or is there some better approach out there?
Why isn't the picker being populated with the results of the ajax request? Is my use of itemTpl incorrect?
Am I setting the form fields in a sencha-appropriate way?
How should I dismiss the picker on itemtap? I somehow doubt that my use of hide() is the proper way.
Picker slots has an data config which is an array of objects. It should have a specific format with text and value.
slots :[
data : [{
text : someValue,
value : someValue1,}] ]
You need to add objects which has the fields text and value to slots.