I am new canJs in general. I am having a hard time understanding the differences between can.fixture and can.observe and how they are both used?
To my understanding can.observe just listens for a function and keeps watching it, and it takes two parameters. As for fixture i am really lost because i keep on hearing about steal.
They are quite different:
Observes are observable objects where you can receive an event whenever a value changes:
var ob = new can.Observe({
name: 'Test'
});
ob.bind('change', function(ev, attr, how, newVal, oldVal) {
console.log('Something on ob changed, the new value is: ' + newVal);
});
ob.attr('name', 'Changed');
This can be useful in many places and makes view bindings possible (so when rendering a view with an Observe, your HTML will change whenever you update the Observe automatically). Read more on it in the documentation.
Fixture just emulate AJAX responses and can help you to test your application or implement functionality even if you REST backend isn't up and running yet:
// Static fixture
can.fixture("tasks", "fixtures/tasks.json");
can.ajax({
url: 'tasks',
dataType: 'json'
}).done(function(data) {
// data is the content of fixtures/tasks.json
// instead of making a request to tasks
});
// Dynamic fixture
can.fixture("/foobar.json", function(original, response){
response(200, "success", { json: {foo: "bar" } }, {})
});
Where both come together is when using Models. Models are basically just observes that get their data from a REST backend. More on Models and fixtures.