What is the proper way to manage related objects in javascript?

I am new to object oriented programming and slowly learning how to apply it to javascript. So please bear with me. :)

I have two main objects:

  • A “record” that contains methods for editing a single record from a set of records. (create, save, load, etc.)

  • "recordList", which contains the output methods of the paginated list of record names.

I would like these objects to work together. For example, if record.save () is called, recordList.refresh () is also called, so the broken list reflects the updated data.

To do this, I created a third “control” element, which contains instances of both “record” and “recordList”. I use "control" as follows:

control = {} control.record = object.create("record"); control.recordList = object.create("recordList"); control.save = function() { this.record.save(); this.recordList.refresh(); }; 

It works. But I wonder, right? (I want to make sure that I don’t violate any OO design rules at the same time.) Is there a better way?

Thanks in advance for your help.

+7
javascript oop
source share
4 answers

Speaking from the point of view of OOP, I do not think that the record will save itself. A record in a database is just data, and the database itself is what does things with that data, whether it's saving or loading, etc. Saying that I will record just an object that stores data, and create a record set object to interact with the data. Inside this record set object, you can put your list of records and update it accordingly. Something like:

 var recordset = function() { var me = this; var currentRecord = object.create("record"); var recordList = object.create("recordList"); me.save = function() { //Insert record.save code here recordList.refresh(); }; }; 

Something to mark this code. In this setting, currentRecord and recordList cannot be accessed from outside the function, and therefore you have encapsulation, one of the hallmarks of OOP. This is because the recordset function is a closure that "closes" all the variables inside, which means that each function inside has access to the variables within the scope of the recordset.

You can allow the outside world to access through the get or set functions:

  me.getRecordList = function() { return recordList.getArray(); //Not generally a good idea to simply return your internal object }; 
+1
source share

Your decision is in order. Two minor suggestions for improvement

  • Use a more specific name than control (even "recordControl" is ok). You can get many controls for different sets of functions.

  • Use the object literal to create the entire object. Keeps your code tidier and easier to read (and saves a few bytes)

(apologies for the lack of interval - the editor does not do what I want!)

  recordControl = { record : object.create("record"), recordList : object.create("recordList"), save : function() { this.record.save(); this.recordList.refresh(); } } 
+1
source share

If this is one thing that I have learned over time, then after any writing paradigm there will be more frustration and difficulty than accepting the concept of how much you can go and using common sense to dictate your deviations.

However, your solution will work fine, and it’s fine to create a container class for several objects of various types that are related. If you want to handle it differently, check out the “Client Pool” . The only thing I can say about what you did is make sure that you use object.create as it was intended.

Using this method, you can create an event that, when launched, will execute a number of other commands related to your event. I used this with great success in all kinds of applications, from the supposed connection to events, to simplify the built-in javascript injections after the postback.

Good luck.

0
source share

why don't you include a record in a RecordList?

 var record = object.create("record"); record.recordList = object.create('recordList'); record.save = function(){ /* do something */ this.recordList.refresh(); } 
0
source share

All Articles