Using the same backbone relational models to work with both pre-loading and ad-hoc loading of data - backbone.js

I have a tree of models and I'd like to have them load from one big JSON request up front and then be able to change them one at a time without saving the whole tree or reloading the whole tree and without making two versions of each model.
The problem I've encountered is if each of the submodels has a keySource they won't upfront load, but without a keySource you can't do an individual load or save.
My content in the upfront load is the entire object tree fully connected (no id lists) because I didn't see a way around this. Is that the problem? Or is what I'm trying to do just not possible without two versions of the models that are somehow connected?

It is possible to do selective saving if you override the save method of Backbone.Model. I wrote an article on this topic. Your choice if you want to create little sub-models that represent parts of your model. When those are changed, you can get their changedAttributes hash and pass it to the save of the main model. With the method I discuss in the article it is entirely possible to post only parts of your model during a save.
Have you considered using a collection for this? You could override the parse method of collection to create your models. Just a thought.

Related

Approach for building a Gallery of images (ParentalKey vs StreamField)

I'm trying to decide between using ParentalKey or StreamField, in this case, with the purpose of adding an image gallery to a page.
We don't need any other information than the image itself (given that the image will be anyway a wagtailimages.Image model's instance, so the alt text is already handled there).
Any thoughts on what is better to make it easier for the editor to work even if maintaining around 50 images per page?
And about good practices and code maintainability?
Would you prefer a third party package for the image gallery? (even if that blocks you from upgrading to wagtail 4?)
Would your opinion change if instead of a single image, we needed some more fields?
Many thanks!
For an image gallery, the first recommendation would be to use the Collections feature. You can get pretty far with the nested collection system and even add extra meta data if needed by adding a model that relates to the collection.
If that is not suitable, ParentalKey/InlinePanel would be my next pick. For simple relationships you get all the benefits of StreamField such as re-ordering, add/remove items but with solid database integrity and usage stats working out of the box.
Only go to StreamField if you need to have optional data set against each image. For example if you have an image list but images could be an Image with a RichText OR just an image.
Unfortunately, managing large sets of images is not great (outside of collections) so you may find you need to build a seperate UI for this. If that ends up being the case you will find migration of data already in model relations being easier to do or maybe not even needed with something like ModelAdmin.
Hope it goes well, be sure to write a blog post about what you end up doing.
I would use the ParentalKey with InlinePanel for that. It shows you all the images as a list in a more compact way than the StreamField. One can reorder this list.
A StreamField is more expandable in the future. You could add new blocks, like videos or quotes or whatever at any point. If you define each block as StructBlock, you will be able to add whatever you want in the future to these blocks without loosing existing data (also true for the ParentalKey model).
I would not use Collections for image slideshows as you won’t be able to sort the imagas in a collection via the CMS, right? Collections are meant to keep order in the backend, I think.

Database Driven Model Mapping in Java

So, I have a project where I get data from a dozen different sources, some are database objects, most often the data is in different JSON formats, or often XML formats. So, I need to take this disparate data and pull it into one single clean managed object that we control.
I have seen dozens of different posts on various tools to do object to object mapping. Orika being one of them, etc. But the problem is that Orika, like many of these still need solid classes defined to do the mapping. If there is a change to the mapping, then I have to change my class, re-commit it, then do a build and deploy new code ... BTW, testing would also have to be done like any code change. So, maybe some of these tools aren't a great solution for me.
Then I was looking to do some sort of database-driven mapping, where I have a source, a field, and then the new field or function I would like to take it to. So, with a database-driven tool, I could modify the fields in the database, and everything would keep working as it should. I could always create a front-end to modify this tool.
So, with that ... I am asking if there is any database-driven tool where I can map field to field, or fields to functions type of mapping? Drools was my first choice, but I don't know if it is my best choice? Maybe it is overkill for my needs? So, I was looking for advice on what might be the best tool to do my mapping.
Please let me know if you need any more information from me, and thanks for all the help!
Actually Orika can handle dynamic Data source like that, there is even an example on how to convert from XML Element (DOM API) or even JsonObject.
You can use an XML parser to convert your data into Element object, or Jackson to get JsonObject
Then define you class map between your "Canonical" Java Class and these dynamic "Classes"
http://orika-mapper.github.io/orika-docs/advanced-mappings.html Customizing the PropertyResolverStrategy
Here is an example of Orika mapping to MongoDB DBObject to Java Bean
https://gist.github.com/elaatifi/ade7321a1405c61ff8a9
However converting JSON is more straightforward than XML (the semantic of Attributes/Childs/Custom tags do not match with JavaBeans)

Creating different models for a view in Backbonejs

I've an web application written using Backbonejs which consists of two modes
1. Read
2. Edit
I have a view and a corresponding model associated with it.
Following is the behaviour, I'm trying to achieve
Not all properties of model, I'm going to show in the read mode
All properties of model will be shown in edit mode
The view is being given some additional responsibility in edit mode which is being achieved using mixin
I do not want to bring extra information in read mode
I do not want my server side logic to know whether the application is in read or edit mode
Do someone thinks that creating two seperate models for a view depending upon mode and creating altogether different service will solve my issue ?
Since it is the same model's information you are trying to work with, you will need a Read View and an Edit View, but can use the same model and be selective about the attributes you want to deal with in either of these views

Backbone.js - Using a real-time (socket.io) data source as a dynamic model

I have zero experience with Backbone.js currently, and before I start having a good look, I wonder if anybody could advise if it is a good fit for my use case.
I have a dashboard where I will present multiple real-time graphs, with the data source being provided a socket.io.
I would like to use the same data source on multiple pages of the dashboard.
Would backbones's models be a good fit here i.e. setup a model that uses the socket.io data source, and then makes it available to all views?
Basically whenever socket.on is called, i need an object in each of the views to be updated.
Is this possible?
Any thoughts would be much appreciated.
Best regards, Ben.
Yeah, that's possible. There's a library for Backbone that replaces the Backbone.sync method with an implementation that works over Socket.IO:
https://github.com/logicalparadox/backbone.iobind

CakePHP: Best practice for app-wide media-uploads

I'm trying to make an App-wide media-upload which should have the possibility of being accessible from every controller/model.
I thought of on table media which holds record of all uploaded files, my schema looks like this:
id
controller //to keep the reference from which controller the file was uploaded
foreign_key //since files should be uploaded to specific records, I need this id
filename
extension
fullname
size
created
modified
I'm not sure what would be the best approach in doing this. I've thought of components, plugins and a behavior but still am unsure.
My App has many different controllers with different records.
For example it manages projects and should now be able to attach PDFs to specific projects from within the project-edit mask.
Since this is a feature needed by other controllers, too I want to write it application wide.
I'm pretty sure I need a helper to call the upload-function from within the masks.
May something like: echo $this->Media->uploadMask(); which provides me with an ready uploading-mask for the controller and id I'm editing at the moment.
But I don't know which route I should call for the upload. Something like /media/upload would be very good, but I'm not sure if this fits correctly into the MVC-approach.
Would it be better to call it from my specific controllers? Or is an AJAX-upload to just a normal controller/model like better?
What you are describing is a Behaviour, which is basically an object of methods that can apply to any model. For controllers there are also Components.
There are already a couple of established upload behaviours for CakePHP you should check out: Meio Upload which is good for basic image manipulation and also the CakePHP Media Plugin which is more advanced and more recently updated.

Resources