Why isn't my ExtJS Store Association Working - extjs

I'm having issues. I want to use the nice ExtJS associations, but they're not working properly.
Issues:
no association showing in the model
no data showing up after load
What are the quirks to watch out for?

I recently went through a very painful learning curve with the ExtJS associations, and came across some useful articles, as well as my own gotchas. Here is the summary for those who run into the same pains.
Rules for HasMany Associations in ExtJS
Always put your Proxies in your Models, not your Stores, unless you
have a very good reason not to [1]
Always require your child models if
using them in hasMany relationships. [2]
Always use foreignKey if you want to load the children at will
Always use associationKey if you return the children in the same response as the parent
You can use both foreignKey and associationKey if you like
Always name your hasMany relationships
Always use fully qualified model names in your hasMany relationship
Consider giving the reader root a meaningful name (other than "data")
The child model does not need a belongsTo relationship for the hasMany to work
[1] The store will inherit its model's proxy, and you can always override it
[2] To make it easy, and avoid potential circular references, you can require them in app.js
http://extjs-tutorials.blogspot.com/2012/05/extjs-hasmany-relationships-rules.html
Rules for HasOne and BelongsTo Associations in ExtJS
Put the proxy in the model, unless you have a very good reason not to
Always use fully qualified model name
Always set the getterName
Always set the setterName
Always set the associationKey, if the foreign object is returned in the same response as this object
Always set the foreignKey, if you want to load the foreign object at will
Consider changing the instanceName to something shorter
The getter behaves differently depending on whether the foreign object is loaded
or not. If it's loaded, the foreign object is returned. Otherwise,
you need to pass in a callback to get it.
You should set the name property if you plan to override this association.
You do not need a belongsTo relationship for a hasMany to work
Set the primaryKey property if the id field of the parent model is not "id"
Sometimes you need to use uses or requires for the belongsTo association. Watch
out for circular references though.
Calling setter() function does
not seem to set the instance. Set object.belongsToInstance = obj if
calling the setter().
http://extjs-tutorials.blogspot.com/2012/05/extjs-belongsto-association-rules.html
Misc
If you're applying your data to a grid, make sure you call reconfigure() on the grid using the new store
Your "foreignKey" property will be applied as a local filter to the ExtJS store; if you see the data loading over the network, but
not showing in your grid, make sure your model has the foreignKey
value defined as a field, or the local filter will exclude the data
quiety. To test if this is the case, hook into the store's "load"
event and call store.clearFilters(), and see if your data shows up

Related

Loading sonata_type_colletion field via ajax

I'm using sonata admin in my project I have two entities, Order and Delivery which are related in a oneToMany relationship. Each of these has a oneToMany relationship with OrderItem and DeliveryItem respectively.
I would like to make it easier when creating a Delivery by loading corresponding Order Items by pre-populating the items delivered through an ajax call based on Order item selected.
I extended the edit theme and can make the ajax call which populates fine, however when trying to save, I get the error in relation to my form having extra fields even though I've followed sonata admin's naming convention to the letter.
Is there a way to do this?
when calling formMapper->add() you can pass the 'allow_extra_fields' => true option.
http://symfony.com/doc/current/reference/forms/types/form.html#allow-extra-fields

How to provide model configuration presets in Django

so what I am trying to achieve is the following:
I have two models A and B which have a OneToOne relationship. Instances of model A are always created before their respective model B instances. Dependent on a parameter, I want to be able to initialize an instance of model b with different initial values.
What I've come up with is to define a ProtoTypeModel and subclass it with the actual Model like so:
from django.db import models
#other imports
class PrototypeB(models.Model):
#define all fields
class B(PrototypeB):
pass
By using dict = PrototypeB.objects.filter(**my criteria).values()[0] or a custom Serializer from Django Rest Framework, I will get a dict which I can then use to instantiate my instance of model B : B.objects.create(**dict).
Is this the proper way to do so or am I missing a huge point?
Best,
D
In case anyone stumbles upon the same question on his path, here's how I did it.
First I thought about implementing the solution I mentioned above. However, researching about multi table inheritance, I found that this is actually a non functioning one.
The multi table inheritance will create an implicit OneToOne _ptr field, which will trigger cascade deleting behavior, causing my arche type models to be deleted, when an instance of B is deleted.
I went along implementing two models PrototypeB and B which works perfectly fine.

Assigning field values before saving on different models

I continue building my CakePHP application. Now I've created all the database schema in MySQL, run some "cake bake all" and I have lots of models, views and controllers, and I'm gonna personalize them.
In many of my models I have this fields:
company_id
created_by
modified_by
As you can understand, the first field is the id of the owner of the "row", created_by is the id of who created the row and modified_by the latest person who updated it.
I know I can create a beforeSave filter in the model and update all the data (I suppose that I can know if I'm creating or updating a row, isn't it?), but just now I have 15 different models and I hope the app will grow more, so it's a lot of repetitive code to write. And, it breaks the DRY principle.
And my question is: Is there any "elegant" way to solve this problem? Maybe creating a class extending AppModel with a beforeSave filter for updating the fields, and that all my models inherit from the new Model class instead of AppModel?
Thanks for your help!
Make it a behaviour and load it for models that need that functionality.
See http://book.cakephp.org/2.0/en/models/behaviors.html#creating-behaviors
I think the most appropriate way is to create Behaviors.
You can set up the beforeSave callback in the behavior like what you have in your model.
Here is the link to the documentation http://book.cakephp.org/2.0/en/models/behaviors.html#behavior-callbacks
You can also check as example dereuromark's WhoDidItBehavior.

CakePHP: Trackable Behavior with associated models

I'm trying to use trackable behavior in CakePHP but with associated models.
I've taken TrackableBehavior as a Model Behavior from Croogo which is based upon a plugin from Jose Gonzalez.
It works like a charm, but I'm trying to find a way to make it work properly when the data of the child/associated model are returned in a view of the "parent" model.
The associations are:
Clients hasMany Notes
Notes belongTo Clients
To be more clear: Both Models use Trackable Behavior. In the Clients "view" view ( ugh ) I list all the notes that are owned by the Client. I'd like Trackable Behavior to return the trackable records ( created_by / modified_by ) for each note that is listed, yet it only returns the Trackable Array for the parent model ( Clients ).
Any ideas?
A bit embarrassing. It was as simple as adding this before using the find() method
$this->Model->recursive = 2;
so CakePHP would go a level deeper to fetch the associated data for each item.

How to identify a BackboneJS Model serverside

I'm just getting my head around BackboneJS, but one of the (many) things I'm still struggling with is how exactly the Models Sync up and relate to the serverside DB records.
For example, I have a Model "Dvd", now I change an attribute on the "Dvd" Model, the Name for example, and then I call save(), how exactly does the server side know what DB record to update? Does Backbone hold the DB row ID or something else?
Yes, typically you will set up your Backbone models so that they have database row IDs for any objects you're working with. When one of them is brand new on the client side and not yet saved to the server, it will either have a default or no ID, which doesn't matter since the server will be assigning the ID if and when the initial save transaction succeeds.
Upon saving or updating a model item, Backbone expects the server to reply with some JSON that includes any attributes that have changed since the save or update request was made. In the response to the initial save request, the server informs the client of a newly saved item's row ID (and you can also send along any other information you might need to pass to the client at the same time).
By default, the 'id' attribute of a model object is assumed to be its unique identifier, but backbone lets you change this if you're using a different identifier for the primary key. Just give your model an idAttribute parameter (see the docs for Backbone.Model.extend()) to do that.
Meanwhile, either the urlRoot parameter or a url function can be given to your models to characterize the urls that should be used to send the various ajax requests to the server for a given model.

Resources