...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
June 16, 2013

Call function inside static function of the class

Kriss’s Question:

In laravel framework i often use – Model::find($id)->orderedBy(param);

I would like to know how to achieve such expression. I began with this:

 Class Test
 {
    static public function write()
    {
       echo "text no 1";

       function second()
       {
          echo "text no 2";
       }
     }
 }

now i when i do

Test::write();

i get “text no 1”

what i want is to do:

Test::write()->second();

and get ” text no 2″

Unfortunately my way doesn’t work.

Is it possible ?

Excuse me for bad language – still learning.

Logically it is not possible, you cannot call second() until you have called Test::write() and you can call it later because afterward the PHP will redeclare the function. So you need to change your approach.

If you return an object from the write() method it is possible.

Class Test
 {
    static public function write()
    {
       echo "text no 1";
       return new Test(); //return the Test Object
    }

    function second()
    {
          echo "text no 2";
    }

 }

Now you can call Test::write()->second();

Read more
June 15, 2013

.fadeto() not working for dynamic class in jquery

User2488882’s Question:

My Code is like this.

<script>
var i=0;
$(window).load(function() {
  $(function() {
   setInterval(update, 1000);
  });
  function update() {
  i++;
  setTimeout(function(){$("#container").after($('<div>',{text:'Hello',class:'test'+i}))},100);
   $("#container .test"+i).delay(500).fadeTo('slow',0);
  }
});
</script>

.after() seems working.
But .fadeto() doesn’s work.

I know there are utilities for dynamic classes like .on() and .live() .
But I dont know how can .fadeTo be working for dynamic classes.

Any ideas?

Try it this way:

$("#container").find(".test"+i).delay(500).fadeTo('slow',0);
Read more
June 14, 2013

Is there a CSS-only fallback for calc() for IE8

Derek Henderson’s Question:

I know there is a CSS fallback for calc() for IE6-7. Likewise, I know there is a jQuery alternative.

However, is there a CSS-only fallback for calc() for IE8? If so, what is it?

There is no support dynamic properties above IE 8

Dynamic properties (also called “CSS expressions”) are no longer supported in Internet Explorer 8 and later, in IE8 Standards mode and higher.

Source: Read this

Read more

Is there a way to temporarily override all click bindings on an element using jQuery?

Lokesh Suthar’s Question:

event.preventDefault() will override default event behavior of an element. How can I temporarily override all click bindings and not just default ones?
Or is there a way to save all the click bindings so I can unbind them and use them later?

Simply as this

$("*").on('click', function(e) {
    //override
    e.stopPropagation(); // To stop event bubbling further
});

To make this work temporarily, you can add a flag like

var override = true;

Now, check this variable before overriding the

$("*").on('click', function() {
    if(override) { 
         //override
         e.stopPropagation();
    }
});
Read more
June 13, 2013

function inside and outside subclass definition

Arachide’s Question:

Below are some code from cocos2dx-js binding official javascript sample

cc.Class = function(){};
cc.Class.extend = function (prop) { //John Resig's extend
    var _super = this.prototype;

    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
    fnTest = /xyz/.test(function(){xyz;}) ? /b_superb/ : /.*/;

    // Copy the properties over onto the new prototype
    for (var name in prop) {
        // Check if we're overwriting an existing function
        prototype[name] = typeof prop[name] == "function" &&
            typeof _super[name] == "function" && fnTest.test(prop[name]) ?
            (function (name, fn) {
                return function () {
                    var tmp = this._super;

                    // Add a new ._super() method that is the same method
                    // but on the super-class
                    this._super = _super[name];

                    // The method only need to be bound temporarily, so we
                    // remove it when we're done executing
                    var ret = fn.apply(this, arguments);
                    this._super = tmp;

                    return ret;
                };
            })(name, prop[name]) :
            prop[name];
    }

    // The dummy class constructor
    function Class() {
        // All construction is actually done in the init method
        if (!initializing && this.ctor)
            this.ctor.apply(this, arguments);
    }

    // Populate our constructed prototype object
    Class.prototype = prototype;

    // Enforce the constructor to be what we expect
    Class.prototype.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;

    return Class;
};


var Explosion = cc.Sprite.extend({
   tmpWidth:0,
   tmpHeight:0,
   active:true,
   ctor:function () {
   this._super();

   //blahblah
   },
   destroy:function () {
      //blahblah
   }
});

Explosion.sharedExplosion = function () {
   //blahblah
};

Just wonder why sharedExplosion is putted outside the definition of var Explosion

It’s a simple extension of Explosion outside its declaration. It is just an implication, I don’t think there has to be particular reason for doing it.

Read more
June 12, 2013

jquery click(), live() and on()

Adam’s Question:

just interested to know which of – Click(), Live() and On() is currently the preferred method to catch a user mouse click?

I assume live() is out as this I believe scans to much of the DOM.

thankyou

From jquery docs:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to
attach event handlers. Users of older versions of jQuery should use
.delegate() in preference to .live(). This method provides a means to
attach delegated event handlers to the document element of a page,
which simplifies the use of event handlers when content is dynamically
added to a page. See the discussion of direct versus delegated events
in the .on() method for more information.

The .on() method attaches event handlers to the currently selected set
of elements in the jQuery object. As of jQuery 1.7, the .on() method
provides all functionality required for attaching event handlers.

For earlier versions, the .bind() method is used for attaching an event handler directly to elements.

TLDR: Always use on() , never use live()

live() and .bind() methods are deprecated. live() has also been removed where as other two are two totally two different types of event handlers.

  1. .click() is a standard event handler

    .click(function() {
        //Applies to DOM already present
    });
    
  2. .on() is used when you require event delegation. This replaced traditional bind() and delegate()

    .on('click', function() {
            //Delegates to each element is DOM as it manipulated or generated
    });
    
Read more
June 11, 2013

use jQuery/jQuerymobile .text to set content of multiple id's

Chris’s Question:

I have a page with multiple page(s- data-roles), and want to set the content on div’s, or other elements. Basically this works… like this:

<a href="..something" id="content-something"></a>
<div id="content-sometext"></div>

and setting it with that:

$('#content-something').text('Something in there');
$('#content-sometext').text('Sometext in there');

The problem occurs when I try to set the content over multiple instances of the id’s, and only the first id is being set – whether in the same page- data role or not:

<a href="..something" id="content-something"></a> ---> Something in there
<div id="content-sometext"></div> ---> Sometext in there
Some more html...
<a href="..something" id="content-something"></a> ---> empty!
<div id="content-sometext"></div> ---> empty!

As far as I understood the jQuery/mobile documentation, multiple instances should be able to be set with .text().

What am I doing wrong?
Thanks a lot for your help in advance 🙂

That is an expected behavior as IDs are suppose to be unique. Use class name to replace text of multiple elements.

<a href="..something" class="content-something"></a>
<div class="content-sometext"></div>
Some more html...
<a href="..something" class="content-something"></a>]
<div class="content-sometext"></div>
Read more

Php array duplicates separation

Joshua Fabillar’s Question:

In PHP I have a arrays of id

$ids = array(1, 1, 1, 2, 2, 3, 3);

I just wanted to separate the 1 which will become 1, 1, 1 and 2 become 2, 2 and 3 become 3, 3.
How can I do that in PHP?

You can do this

$ids = array(1, 1, 1, 2, 2, 3, 3);
foreach($ids as $key) {
    //Calculate the index to avoid duplication
    if(isset(${"arr_$key"})) $c = count(${"arr_$key"}) + 1;
    else $c = 0;
    ${"arr_$key"}[$c] = $key;
}

Demo

Read more
June 7, 2013

How to execute a simple PHP Code?

Max’s Question:

This basic statement doesn’t work when i execute on my browser.

<?php echo "Helloworld"; ?>

I saved this statement in a file index.php and used a browser to execute. What mistake might be going through? I am new to php and please help me through it. Thanks in advance.

EDIT

Apart from syntax please tell me why this isn’t working when i save it in a .php file and open in a browser this doesn’t show anything?

PHP start tag, should be <?php and end tag should be ?> without any SPACES.

If even after removing the spaces the PHP is not working. You have to make your you can execute your PHP script from a server with application capable to executing it. Like Apahce, IIS. You can find thise application bundled with following stacks: LAMP, XAMPP, WAMP, MAMP, .IIS

Read more
June 4, 2013

Best method to get double value from datetime in jquery

User2451211’s Question:

Which is the best way to convert datetime to double in jquery? I have used Date.parse. But, it is not correct in few cases. Also,i am unable to pass datetime format for that method.


Viji

In JavaScript, you can simple do variablename.toFixed(2); for that.

Example:

var num1 = 12312.12312
console.log(num1.toFixed(2)); // Will give 12312.12
Read more
...

Please fill the form - I will response as fast as I can!