I would like to make a relation between two models User and Task using backbone-relational.
I would like for each Task to get the User model.
The relation between the two model is the following:
taskModel.creator_id = userModel.id
Here is my code (1) (2)
The issue is the following (3):
When I try to fetch the models of task I get for the attributes.user the null value.
(1)
// TaskModel
var Task = Backbone.RelationalModel.extend({
relations: [
{
type: 'HasOne',
key: 'user',
relatedModel: User
}
],
urlRoot: 'url_get_tasks'
});
(2)
// UserModel
var User = Backbone.RelationalModel.extend({
urlRoot: "url_get_users"
});
(3)
user = new User();
user.fetch();
console.log(user.attributes); // {id: 1, .....}
task = new Task();
task.fetch();
console.log(task.attributes); // {id: 12, creator_id: 1, user: null} ???
Why the task.attributes.user have the null value?
Your relation on Task should be something like:
{
type: 'HasOne',
key: 'user',
keySource: 'creator_id',
relatedModel: User
}
(You're expecting it to use creator_id for the association but never told it that.)
Related
I am having trouble loading my first ExtJS store.
var testStore = Ext.data.StoreManager.lookup('myUserStoreID');
testStore.load({
callback: function (records, operation, success) {
testStore.each(function (record) {
debugger;
var task = record.data.description;
console.log(task);
});
//debugger;
//var x2 = operation._response;
//var x3 = x2.responseText;
//var x4 = Ext.decode(x3);
////var x3 = Ext.JSON.decode(operation);
////var x2 = Ext.decode(operation.response);
//console.log(testStore);
}
});
In debugger I can see the correct data if I drilldown into operation._response.responseText, but the records are blank. So I know it just has to do with my code. If I use the Ext.decode it does return an object. What am I doing wrong that the return data automatically populates my store.
Here is a picture of the object in fiddler.
here is the Model I am trying to use... I know it doesn't have all the fields yet.
Ext.define('ExtApplication1.model.UserModel', {
extend: 'ExtApplication1.model.Base',
requires: ['ExtApplication1.model.Base'],
fields: [
/*
The fields for this model. This is an Array of Ext.data.field.Field definition objects or simply the field name.
If just a name is given, the field type defaults to auto. For example:
*/
{ name: 'UserID', type: 'int' },
{ name: 'UserName', type: 'string' },
{ name: 'password', type: 'string' },
{ name: 'Email', type: 'string' },
{ name: 'GroupID' }
],
proxy: {
type: 'ajax',
url: 'http://localhost:49537/api/user/gettargetuser/xxx/xxx',
reader: {
type: 'json',
rootProperty: 'JSON'
}
}
Here is my User class in webapi
here is the MainModel.js where I create stores
Ext.define('ExtApplication1.view.main.MainModel', {
extend: 'Ext.app.ViewModel',
alias: 'viewmodel.main',
data: {
name: 'ExtApplication1',
appName: xxx,
appHeaderIcon: '<span class="fa fa-desktop fa-lg app-header-logo">',
footer: 'Copyright - xxx- x
},
stores: {
sessionListByInterest: {
model: 'MainMenuListModel',
autoLoad: false //was true
},
myUserStore: {
model: 'UserModel',
storeId: 'myUserStoreID',
autoLoad: false
},
The issue is that the store expects rootProperty to contain an array of records, while you are only delivering a single record.
If you want to only load a single record, which is not in an array, you would have to use the static model.load() function.
Or you can change your API endpoint from User JSON to List<User> JSON (or IEnumerable<User> or ICollection<User> or User[]...), even if you intend to only return a single user. Doesn't matter how many records there are in the JSON array, but the array is expected by ExtJS store.
exampleData = {
id: 1,
name: 'A',
house: {
address1: 'California'
address2: 'California'
}
}
House = Backbone.RelationalModel.extend({
urlRoot: function urlRoot() {
var personId = this.get('person').id; // this.get('person') is null
}
});
Person = Backbone.RelationalModel.extend({
relations: [
{ // Create a (recursive) one-to-one relationship
type: Backbone.HasOne,
key: 'house',
relatedModel: House,
reverseRelation: {
type: Backbone.HasOne,
key: 'person'
}
}
],
initialize: function() {
}
});
I need to get the person's model data in House model,
but this.get('person') returns null, even I set up the reverseRelation.
How can I get the Person's data in house?
I think your code will not work. The definition of model is a bit wrong. When you define relation, it should be reference to the object, not the string value:
relatedModel: House // instead of 'House' as a string
Then you may reference through the getters, from each of the models, alike:
houseModel.get("person");
personModel.get("house");
Let's suppose I have the following model (model1) and collection (collection1)
model1.attributes = {
name: 'bar'
};
collection1.models = [{}, {}, {}];
It will be possible by using backbone relation to make the model1 to know about the length of collection1?
model1.attributes = {
name: 'bar',
collection1Length = 3 // collection1.models.length
}
Thanks
Based on your comments, it might be best to simply create a reference to the collection itself within the model:
ModelName = Backbone.Model.extend({
...
linked_collection: null // don't call this 'collection', as model.collection already exists
...
}
var model1 = new ModelName();
model1.set('linked_collection',collection1);
Now you can do this at any time to get the linked collection's length.
model1.get('linked_collection').length
I have 2 models - for example - Users and Orders
Ext.define('AM.model.User', {
extend: 'Ext.data.Model',
fields: ['id', 'username', 'firstName', 'lastName', 'state', 'city'],
associations: [
{
type: 'hasMany',
model: 'Order',
name: 'orders'
},],
});
Ext.define('AM.model.Order', {
extend: 'Ext.data.Model',
fields: ['id', 'userId', 'date', 'description', 'value'],
belongsTo: 'User',
});
and their stores. I'm looking for a way to display data from both stores in grid. (so my columns would be firstName, lastName, orderDate, orderDescription, orderValue...
What is the proper way to display them?
Thanks.
You should do this with your server side code and get this all data into a single store.
If you want to do this with associations, you need to define a renderer for your grid column.
Like so:
{
text: "orderDescription",
renderer: function(value,meta,record){//1.
//var orderStore = record.orderStore;//2a.
//var orderList = ordersStore.data.items;//2b.
var orderList = record.orders().data.items; //3.This line is the same as 2 the lines above(2a,2b).
var order = orderList[0]; //4. TODO..loop through your list.
var description = order.data.description; //5.
return description ;
},
I will try to explain for anyone who wants to do it this way.
The third parameter is the current record in your 'User' Store being renderd for the grid. The first two just need to be there, to receive record. Leaving them out will not work.
1a/1b. I left these there to demonstrate how the association works. Basically, each record in your 'User' Store gets its own corresponding 'ordersStore'. It will be called 'ordersStore', because you put in your association [name: 'orders'].
The orders() method is automatically created, again based on the name 'orders'. This just returns the orderStore. The store contains a field data -> which contains a field items. items is the actual list of orders.
Now you have access to your list of orders. You can loop trough the orders now. Or if you have only one order, just the first one.
Your order again contains a field data which contains your actual data.
Lets try with below example-
Step 1: Adding records from Store 2 to Store 2.
var store2 = new Ext.data.Store({
...
});
var store1 = new Ext.data.Store({
...
listeners: {
load: function(store) {
store2.addRecords({records: store.getRange()},{add: true});
}
}
});
Step 2: Using records from Store 2 with Store 1.
For example, the first data col comes from Store 1 and the data from Store 2 forms cols 2 and 3. You can use a renderer that finds the data in the second store if the 'other' columns are just 'lookup' data, e.g.:
var store1 = new Ext.data.Store({
...,
fields: ['field1', 'field2']
});
var store2 = new Ext.data.Store({
...
id: 'field2',
fields: ['field2', 'fieldA', 'fieldB']
});
var renderA = function(value) {
var rec = store2.getById(value);
return rec ? rec.get('fieldA') : '';
}
var renderB = function(value) {
var rec = store2.getById(value);
return rec ? rec.get('fieldB') : '';
}
var columns = [
{header: 'Field 1', dataIndex: 'field1'},
{header: 'Field A', dataIndex: 'field2', renderer: renderA},
{header: 'Field B', dataIndex: 'field2', renderer: renderB}
];
Best of luck.
Ref. from here
var store = new Ext.data.JsonStore({
id:'jfields',
totalProperty:'totalcount',
root:'rows',
url: 'data.php',
fields:[{ name:'jfields' },
{ name:'firstyear' , mapping :'firstyear' , type:'float' },
{ name:'secondyear', mapping :'secondyear', type:'float' },
{ name:'thirdyear' , mapping :'thirdyear' , type:'float' },
{ name:'fourthyear', mapping :'fourthyear', type:'float' },
{ name:'fifthyear' , mapping :'fifthyear' , type:'float' } ]
}
});
What I want is to add data at the end for this store , but I am totally confused , what I did is I add the following code to it but not working.
listeners: {
load : function(){
BG_store.add([{"jfields":"Monthly","firstyear":22.99,"secondyear":21.88,"thirdyear":21.88,"fourthyear":22.99,"fifthyear":21.88}]);
}
}
But I do not think my concept are cleared ,Please any body show some way how to do it .
You need to define a record type, create it and at it, e.g:
TaskLocation = Ext.data.Record.create([
{name: "id", type: "string"},
{name: "type", type: "string"},
{name: "type_data", type: "string"},
{name: "display_value", type: "string"}
]);
Then:
var record = new TaskLocation({
id: Ext.id(),
type: "city",
type_data: "",
display_value: "Brighton"
});
Then:
my_store.add(record);
my_store.commitChanges();
Remember by the time the data is in the store it's not in the same format as you sent it down but in Record objects instead.
See the recordType property in the JsonStore. It's a function that can be used as a record constructor for the store in question. Use it like this:
var newRecord = new myStore.recordType(recordData, recordId);
myStore.add(newRecord);
I have also figured out a simple solution to this:
listeners: {
load: function( xstore , record , option ) {
var u = new xstore.recordType({ jfields : 'monthly' });
xstore.insert(record.length, u);
}
}
Here what I have to add is this listeners as when the data loads it will create the record type and u can add fields as data as many as u want