Creating multiple instancesof an object in js

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1274 Views - Last Post: 07 June 2015 - 10:36 AM

#1 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Creating multiple instancesof an object in js

Posted 05 June 2015 - 03:21 PM

I've got a div class called application-frame It has other divs inside for an exit button,minimise button and a fullscreen button. The problem is that I want to put it into an arraylist so that I can have two or more instances of application-frame open at the same time. I've actually got this working but even though I am able to set width/height data to it one array object overrides the other giving them both the same dimensions. Also they both fadeout when I click the exit button (instead of one). Any help? thanks in advance.

PS: I use jquery too if it isn't obvious.

////////////front-end.js/////////////

$(document).ready(function() {
	$("#start-menu-frame").hide();
	$('#start-menu-icon').hover(function(){
        $("#start-menu-frame").fadeIn();
    }, function(){
          $("#start-menu-frame").fadeOut();
    });
	
	var applications = [];
	applications.push(new createFrame(200,200));
	applications.push(new createFrame(500,500));
});

///////////applications.js///////////////////////////////

function createFrame(width,height) {
		$("#desktop-box").append('<div class = "application-frame"><div class = "buttons-box"><div class = "exit-button"></div><div class = "fullscreen-button"></div><div class = "minimise-button"></div><div id ="webcontent" class ="content"></div></div></div>');
		$(".application-frame").width(width);  
		$(".application-frame").height(height);  
		$(".exit-button").click(function() {
			$(".application-frame").fadeOut('very slow');
		});
		$(function() {
			$(".application-frame").draggable({containment:"parent"});
		});
	}

This post has been edited by andrewsw: 05 June 2015 - 03:22 PM
Reason for edit:: Added [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Creating multiple instancesof an object in js

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 03:24 PM

Topic moved to jQuery subforum.

How to use code tags:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#3 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 03:47 PM

That's exactly what you're telling it to do, you're telling it to change the width and height property of every element with the class ".application-frame". You shouldn't tell it to do that, you should create the parent element, then add all of the children, and use a reference to that parent element to only target elements with that class that are children of the container. Also, don't create elements with IDs, you can't have more than element with the same ID on the page.

The key is saving a reference to the elements you're creating so that you can access them later instead of just telling it to target every element on the page with a certain class.
Was This Post Helpful? 0
  • +
  • -

#4 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 04:02 PM

@ArtificialSoldier - How would you go about doing that. I already have the div size problem sorted, so I'ts only the other one now.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3134
  • View blog
  • Posts: 8,931
  • Joined: 15-January 14

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 04:41 PM

Like I said, just save a reference to the container element you created, then target only its children. You're using new when you call that function, so it sounds like you're trying to set it up like an object. The container element would be a property of that object. The jQuery documentation doesn't say if append returns the new element, so you might have to create the element first and then append it.

You can use jQuery's find method to target child elements.

https://api.jquery.com/find/
Was This Post Helpful? 1
  • +
  • -

#6 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 05:12 PM

I'm sorry to bother again but could please elaborate, this is for a WebOS and I would like to allow these windows to open and close but really I have now idea.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 05:46 PM

View PostArtificialSoldier, on 05 June 2015 - 11:41 PM, said:

The jQuery documentation doesn't say if append returns the new element, so you might have to create the element first and then append it.

Just to note that, IIRC from a previous forum question, append() does return a reference to the element. I was surprised that this wasn't documented. I think this is because it is supposed to be expected behaviour for a jQuery method.

Added: Actually, I think the question was about remove(), but it still holds.

This post has been edited by andrewsw: 05 June 2015 - 06:06 PM

Was This Post Helpful? 0
  • +
  • -

#8 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 08:49 PM

Thanks for the help. I found a really easy solution.Heres the code if anyone wants the idea:

var app = $('<div class = "application-frame" style="width:'+width+'; height:'+height+';"><div class = "buttons-box"><div class = "exit-button"></div><div class = "fullscreen-button"></div><div class = "minimise-button"></div></div></div>');  
		$("#desktop-box").append(app);
		$(".exit-button").click(function() {
			$(app).fadeOut('very slow');
		});

Was This Post Helpful? 0
  • +
  • -

#9 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 05 June 2015 - 09:14 PM

Sorry after doing some tests I realized that it will delete application-frames based on how many array objects come after it. So if I use position:absolute; in css, they are all on top of each other.When I fade the bottom one (there are three initiated) the other 2 fade as well.When I fade the middle one the top one fades as well. Any ideas.
Was This Post Helpful? 0
  • +
  • -

#10 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 06 June 2015 - 04:23 AM

Update: got it working using:
 var app = $('<div class = "application-frame" style="width:'+width+'; height:'+height+';"><div class = "buttons-box"><div class = "exit-button"></div><div class = "fullscreen-button"></div><div class = "minimise-button"></div></div><div class = "content"></div></div>');  
		var apps = [];
		$("#desktop-box").append(app);
		apps.push(app);
		$(".exit-button").click(function() {
		for(var i = 0; i <apps.length;i++) {
			apps[i].pop($(app).fadeOut('very slow'));
			console.log(apps.length);
	    }
get this error though:

Uncaught TypeError: apps[i].pop is not a function

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Creating multiple instancesof an object in js

Posted 06 June 2015 - 04:34 AM

apps[i] is an array element, it doesn't have a pop() method.

pop is used as someArray.pop() with no arguments. MDN reference:

Quote

The pop method removes the last element from an array and returns that value to the caller.

Was This Post Helpful? 0
  • +
  • -

#12 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 06 June 2015 - 09:04 AM

Oh! This is so annoying. Now I cant use:
$(app).resizable({maxHeight: 678},{maxWidth: 1361});

But I can use:
$(app).draggable({containment:"parent"});
properly?
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Creating multiple instancesof an object in js

Posted 06 June 2015 - 09:14 AM

It is more likely to be
$(app).resizable({maxHeight: 678, maxWidth: 1361});

Was This Post Helpful? 0
  • +
  • -

#14 TheDude0806   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 54
  • Joined: 05-June 15

Re: Creating multiple instancesof an object in js

Posted 06 June 2015 - 12:12 PM

Sorry, code error. Still not working.
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Creating multiple instancesof an object in js

Posted 06 June 2015 - 12:57 PM

What errors appear in your browser's console?

View PostTheDude0806, on 06 June 2015 - 07:12 PM, said:

Sorry, code error. Still not working.

Erm, this isn't adding any useful information.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2