Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jquery in ember app

I'm building an app with ember js and I'm not sure how to use jQuery with ember. An example of what I'm trying to achieve is a page that gets sale data from my api.

in my app.js I've got

App.TransactionsRoute = Ember.Route.extend({
    model: function() {
        return App.Transaction.find();
    }
});

If I pass in a start date and an end date to the App.Transaction.find() method my api will restrict the data it sends back to within those dates.

I have a form on the page with an input for the start and end dates and I want to hook this up to jQuery UI's datepicker.

This is where I'm stuck I put this at the end of my ember app code

jQuery(document).ready(function() {
    jQuery("#transactionsStartDate").datepicker();      
    jQuery("#transactionsEndDate").datepicker();
});

but it doesn't do anything and no errors are thrown.

How can I get the jquery to run? also how do I hook up the inputted date variables to the call to App.Transaction.find({startDate: startDateVariable, endDate: endDateVariable})

thanks for your help!

EDIT Correction the jQuery is running but it's running before the view is rendered. Does ember have a hook or something that I can call when my view is rendered?

like image 524
Devin Crossman Avatar asked Feb 22 '13 05:02

Devin Crossman


People also ask

Does Ember use jQuery?

Ember has been historically coupled to jQuery. As part of RFC294, jQuery has been made optional. This addon makes jQuery available in an Ember project. It also provides the mechanism that implements jQuery integration when that feature is enabled.

Is Ember js still used?

Ember has been an enabler of great productivity for many teams for almost a decade and I'm sure it's going to continue to be that. It's changed and improved a lot since its first release and is now in better shape than ever with its Octane edition.

Is Ember js any good?

Ember is considered to be one of the best in distributor logic. It provides the best combination with ember-data and the best CLI, which makes working with Ember much easier. Almost all programmers love the default architecture provided by Ember. React JS does not provide any architecture for its own.

Is Ember js a JavaScript framework?

Ember. js is a productive, battle-tested JavaScript framework for building modern web applications.


1 Answers

Instead of putting the datepicker initialization in jQuery(document).ready() hook into the didInsertElement method of the view

App.TransactionsView = Ember.View.extend({
    templateName: 'transactions',
    didInsertElement: function() {
        jQuery("#transactionsStartDate, #transactionsEndDate").datepicker();    
    }
});

use Ember's built in TextField view

<script type="text/x-handlebars" data-template-name="transactions">
<form>
    <label for="transactionsStartDate">Start Date</label>
    {{view Ember.TextField id="transactionsStartDate" valueBinding="startDate"}}
    <label for="transactionsEndDate">End Date</label>
    {{view Ember.TextField id="transactionsEndDate" valueBinding="endDate"}}
    <button {{action fetchTransactions}}>Fetch Data</button>
</form>
</script>

and define the action fetchTransactions on the controller

App.TransactionsController = Ember.ArrayController.extend({
    fetchTransactions:function() {
        this.set('model', App.Transaction.find({startDate:this.get('startDate'), endDate:this.get('endDate')}));    
    }
});
like image 114
Devin Crossman Avatar answered Oct 28 '22 15:10

Devin Crossman