Durandal attempts to retrieve a view located within the viewmodel directory

Having an issue with durandal's compose binding as it is looking for views under app/viewmodels instead of app/views

The ViewModel code:

define(["fields/fieldClosures"], function (fields) {
    var ctor = function(opt) {
        this.value = opt.value;
    };
    
    return fields.readonly.default = ctor;
});

The View using compose binding:

<div data-name="fields">
    <div data-bind="css: { 'has-error': hasError }" class="form-group">
        <label data-name="label" data-bind="attr: { 'for': id }" class="col-lg-3 control-label"></label>
        <div data-name="field" class="col-lg-9"></div>
    </div>
</div>

The issue arises when data-name="field" gets translated to data-bind="compose: field" and the viewLocator is not able to load the view correctly.

Update: The path to VM is \App\viewmodels\form\fields\readonly\text.js

The VM that holds the field member:

define(["fields/fieldClosures",
    "fields/readonly/text",
    "fields/readonly/date",
    "fields/readonly/number"], function (fields) {
    // Code for factory and constructor injection
    // ...
    return ctor;
});

The references are created using a factory, but the constructors are injected with requirejs as usual.

Another Update:

'fields' is added at the beginning:

requirejs.config({
    paths: {
        'text': '../Scripts/text',
        'durandal': '../Scripts/durandal',
        'plugins': '../Scripts/durandal/plugins',
        'transitions': '../Scripts/durandal/transitions',
        "fields": "viewmodels/form/fields"
    }
});

Answer №1

Did you make sure to activate the useConvention feature on the viewLocator? If not, Durandal will automatically search for the view in the same directory as the viewModel. The following code snippet shows how the translation is done in the viewLocator:

convertModuleIdToViewId: function(moduleId) {
    return moduleId;
}

If you want to enforce a particular view/viewModels setup, you can enable the useConvention option in the viewLocator. Here's a condensed version of the relevant code snippet:

useConvention: function(modulesPath, viewsPath, areasPath) {
    modulesPath = modulesPath || 'viewmodels';
    viewsPath = viewsPath || 'views';

    var reg = new RegExp(escape(modulesPath), 'gi');

    this.convertModuleIdToViewId = function (moduleId) {
        return moduleId.replace(reg, viewsPath);
    };
}

Alternatively, you can customize the view locator by providing your own implementation of convertModuleIdToViewId and integrating it early in your application's lifecycle like so:

var configureViewLocator = function () {
    var viewModelsRegex = /viewmodels/gi;

    viewLocator.convertModuleIdToViewId = function (moduleId) {
        return moduleId.replace(viewModelsRegex, "views");
    };
};

I hope this explanation clarifies things for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error message: It seems the spatial reference for this ESRI object is missing

Currently, I am utilizing Esri GIS to load the center location from an address. However, I am encountering an issue as I am using a geocoder from Google to obtain longitude and latitude, which is resulting in the following error message: TypeError: this.s ...

Create proper spacing for string formatting within an AngularJS modal

I am working with a popup that displays output as one string with spaces and newline characters. Each line is concatenated to the previous line, allowing for individual adjustments. Test1 : Success : 200 Test2 : Su ...

Generating a single JSON record in React Native

Need help displaying a single JSON record from an API request on the screen. const [user, setUser] = useState(); const getUserData = async () => { // {headers: {Authorization: "Basic " + base64.encode(username + ":" + passwor ...

Using Javascript to access a website from a Windows Store application

Currently, I am working on a project to create a Windows store app using HTML and JavaScript. One of the key components of my app involves logging into a specific website with a username and password. Here is an example website for reference: The process ...

The use of callbacks is ineffective in addressing the asynchronous nature of

Hello everyone, I'm currently working on a weather app and I'm facing an issue with the asynchronous behavior of useState. I've come across some suggestions on Stack Overflow that using a callback in the useState function might solve the pro ...

The count of bits is not producing the anticipated result

Attempting to tackle the challenge of Counting Bits using JavaScript, which involves determining the number of set bits for all numbers from 0 to N, storing them in an array, and returning the result Let me provide an explanation Input: n = 5 ...

Adjust hover effects based on true conditions

Currently working on a web app using HTML, CSS, JavaScript, and AngularJS. Progress so far includes a clickable box that triggers a javascript function to display more boxes upon click using ng-click. <div ng-click="!(clickEnabled)||myFunction(app)" cl ...

Caution: Refs cannot be assigned to function components

I'm currently using the latest version of Next.js to create my blog website, but I keep encountering an error when trying to implement a form. The error message reads as follows: Warning: Function components cannot be given refs. Attempts to access t ...

Click on the checkbox to activate it using JavaScript

I'm trying to use JavaScript to toggle the checkbox status when clicking a button. The first button is supposed to uncheck the box and it's working correctly: function clear() { document.getElementById("check").checked = ""; } However, I al ...

The form yields no response and fails to send any data

Ensuring that the form on this site successfully sends the name and phone number is crucial. However, upon clicking the send button, I encounter an empty modal window instead of a response indicating whether the data was sent or not. The contents of the fi ...

When running through Selenium web driver, JS produces inaccurate results

Currently, I am utilizing JavaScript to determine the number of classes of a specific type. However, when I run the JS code in Webdriver, it provides me with an incorrect value. Surprisingly, when I execute the same JavaScript on the Firebug console, it gi ...

Looking for a modular approach? Incorporate specific parts of a module into the parent component

Developing a node.js module and aiming to organize my code effectively. My goal is to have individual files/folders for different parts of the module such as authentication, users, etc. These will be required from a parent package which will then expose t ...

Create a layout using CSS that features two columns. The left column should be fluid, expanding to fill all remaining space, while the right column should be fixed

I need to ensure that the Online Users div always remains at a fixed size of 200px, while allowing the chat window next to it to resize dynamically based on available space. Essentially, I want the chat window to adjust its width as the window is resized, ...

After a two-second period of inactivity, the buttons on the item should trigger an action

I have a scenario in mind that I want to implement: When the user clicks on either the "Plus" or "Minus" button. If the user doesn't click on any of those buttons within 2 seconds, then we assume that the current quantity should be sent to the server ...

Passing data from an API in Vue.js to a different page

I'm a beginner with Vue Js and I'm looking for guidance on how to send data between two components. Currently, I am working on a Vue app that fetches a list of users from an API and displays them. My goal is to transfer data between these compone ...

Is it possible to import a class from a different project or module in TypeScript?

I am attempting to modify the build task in Typescript within this specific project: https://github.com/Microsoft/app-store-vsts-extension/blob/master/Tasks/app-store-promote/app-store-promote.ts I am looking to incorporate an import similar to the one be ...

Tips for adjusting the material ui Popper width to fit the container without disabling the portal

Currently utilizing the material-ui popper library. I am trying to allow the popper to extend outside of its container in the vertical direction. To achieve this, I have set disableportal={false}. However, upon setting disableportal to false, when assign ...

What is the process for accessing a table in indexedDB using Dexie.js immediately after it has been opened?

I am faced with the challenge of needing to verify if a specific table already exists in IndexedDB right after it has been opened. However, I am unsure how to access the DexieDB object inside the 'then' statement. this.db = new Dexie("DBNAME"); ...

Queries with MongoDB RegEx fail to return any matches if the search string contains parentheses

When trying to implement case-insensitivity using regex, it seems to work well for plain strings. However, if special characters like parenthesis are involved in the search query for the name, the database returns no results. For example, a search for "Pu ...

Leveraging jQuery selectors to manipulate data received from an Ajax response

I am encountering an issue with an Ajax call and response. Previously, I had a block of regular javascript code that successfully transformed my select element into a select2 widget. However, when I attempt to convert the select element into a select2 usi ...