Output in html or json?

Hi,

I hope someone could help with my problem. It’s more of a design/pattern problem then actual code.

Short description of the project:

Users can create projects, a project consist of photos. Users can edit photos in a live wysiwyg editor. Output is a zipfile with the photos.

the live wysiwyg editor, is based on jquery, and consists over 200 variables that can be altered using dropdowns/sliders/colorboxes etc. Its javascript heavy.

In my previous design, using codeigniter I relied on ajax and json to send data over to the framework. Only there was almost no data send. Since (i thought) sending data to the editor is nonsense, since when you create view, you already have all the variables. I put the data in javascript classes, thats get read/managed in the view. Something like:


var photos = <?php echo $photos ?>

function Photo(){

	return { 

		attributes: {

			Maxmem: 350,

		},		

		Init: function(args)

		{		

			this.attributes.Maxmem = 			args.maxmem === undefined ? 350 : args.maxmem;

		},

	}

};

then a document ready function reads the photos and instantiates the classes.

This was nessesary because:

  • only save when the user press save (So I need 1 big object with all the information)

  • it’s alot of variables, that get changed and are dependend of eachother. I needed to keep track of everything.

Now with YII!

I decided to convert to yii (reason being, my new job, works with yii, that way I dont have to learn 2 frameworks + yii has gii and is more MVC imo).

Now I come to same issue. How to accomplish communication between javasciprt and php.

Option one: Jsonencode the model to registerScript. The json gets printed in the head. It’s bulky, but I have all the information right in the head.

Option two: use ajax onload to get jsonencoded model. Cleaner, but extra page request(s).

Alsoo, atm I have all kinds of javascript classes, and I wonder if they are nessesary. Basicly the run down is this:

  • view containing json object of the model in a javascript variable

-> gets read with javascript on document ready

-> javascript instantiates the classes. The classes being a simpeler version of the model, with own validation, getters and setters.

-> javascript instatiates the sliders with the value from the json. Alsoo setting a min and max

-> does this 200 times for all values, in about 20 classes.

-> on save, sends the 20 classes.attributes converted to json to the controller.

For me its a clean way, since I can visually see where are variables are going. But its a bit redunded I think… A PHP Class goes to JSON goes to Javascript Class goes to JSON goes to PHP Class. While the structure remains the same.

Alsoo some stuff, like the min/max and the type of value gets put in Javascript class, and are not available in PHP or Database. Im wondering if this is good practice. Although the classes only gets instantiated once, on 1 view, so Its not like I need those values on more places.

It’s not a real complex problem, more a “what do you favor” problem, I guess… Im wondering what your work method is, so I can learn new ideas and methods.